home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270_src.lha / gcc-2.7.0-amiga / gcc.c < prev    next >
C/C++ Source or Header  |  1995-08-24  |  142KB  |  5,282 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.
  20.  
  21. This paragraph is here to try to keep Sun CC from dying.
  22. The number of chars here seems crucial!!!!  */
  23.  
  24. /* This program is the user interface to the C compiler and possibly to
  25. other compilers.  It is used because compilation is a complicated procedure
  26. which involves running several programs and passing temporary files between
  27. them, forwarding the users switches to those programs selectively,
  28. and deleting the temporary files at the end.
  29.  
  30. CC recognizes how to compile each input file by suffixes in the file names.
  31. Once it knows which kind of compilation to perform, the procedure for
  32. compilation is specified by a string called a "spec".  */
  33.  
  34. #include <sys/types.h>
  35. #include <ctype.h>
  36. #include <signal.h>
  37. #include <sys/stat.h>
  38. #include <errno.h>
  39.  
  40. #ifndef _WIN32
  41. #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
  42. #else
  43. #include <process.h>
  44. #endif
  45.  
  46. #include "config.h"
  47. #include "obstack.h"
  48. #ifdef __STDC__
  49. #include <stdarg.h>
  50. #else
  51. #include <varargs.h>
  52. #endif
  53. #include <stdio.h>
  54.  
  55. /* Include multi-lib information.  */
  56. #include "multilib.h"
  57.  
  58. #ifndef R_OK
  59. #define R_OK 4
  60. #define W_OK 2
  61. #define X_OK 1
  62. #endif
  63.  
  64. #ifndef WIFSIGNALED
  65. #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
  66. #endif
  67. #ifndef WTERMSIG
  68. #define WTERMSIG(S) ((S) & 0x7f)
  69. #endif
  70. #ifndef WIFEXITED
  71. #define WIFEXITED(S) (((S) & 0xff) == 0)
  72. #endif
  73. #ifndef WEXITSTATUS
  74. #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
  75. #endif
  76.  
  77. /* Add prototype support.  */
  78. #ifndef PROTO
  79. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  80. #define PROTO(ARGS) ARGS
  81. #else
  82. #define PROTO(ARGS) ()
  83. #endif
  84. #endif
  85.  
  86. #ifndef VPROTO
  87. #ifdef __STDC__
  88. #define PVPROTO(ARGS)        ARGS
  89. #define VPROTO(ARGS)        ARGS
  90. #define VA_START(va_list,var)    va_start(va_list,var)
  91. #else
  92. #define PVPROTO(ARGS)        ()
  93. #define VPROTO(ARGS)        (va_alist) va_dcl
  94. #define VA_START(va_list,var)    va_start(va_list)
  95. #endif
  96. #endif
  97.  
  98. /* Define a generic NULL if one hasn't already been defined.  */
  99.  
  100. #ifndef NULL
  101. #define NULL 0
  102. #endif
  103.  
  104. /* Define O_RDONLY if the system hasn't defined it for us. */
  105. #ifndef O_RDONLY
  106. #define O_RDONLY 0
  107. #endif
  108.  
  109. #ifndef GENERIC_PTR
  110. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  111. #define GENERIC_PTR void *
  112. #else
  113. #define GENERIC_PTR char *
  114. #endif
  115. #endif
  116.  
  117. #ifndef NULL_PTR
  118. #define NULL_PTR ((GENERIC_PTR)0)
  119. #endif
  120.  
  121. #ifdef USG
  122. #define vfork fork
  123. #endif /* USG */
  124.  
  125. /* On MSDOS, write temp files in current dir
  126.    because there's no place else we can expect to use.  */
  127. #ifdef __MSDOS__
  128. #ifndef P_tmpdir
  129. #define P_tmpdir "."
  130. #endif
  131. #endif
  132.  
  133. /* Test if something is a normal file.  */
  134. #ifndef S_ISREG
  135. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  136. #endif
  137.  
  138. /* Test if something is a directory.  */
  139. #ifndef S_ISDIR
  140. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  141. #endif
  142.  
  143. /* By default there is no special suffix for executables.  */
  144. #ifndef EXECUTABLE_SUFFIX
  145. #define EXECUTABLE_SUFFIX ""
  146. #endif
  147.  
  148. /* By default, the suffix for object files is ".o". */
  149. #ifdef OBJECT_SUFFIX
  150. #define HAVE_OBJECT_SUFFIX
  151. #else
  152. #define OBJECT_SUFFIX ".o"
  153. #endif
  154.  
  155. /* By default, colon separates directories in a path.  */
  156. #ifndef PATH_SEPARATOR
  157. #define PATH_SEPARATOR ':'
  158. #endif
  159.  
  160. #ifndef DIR_SEPARATOR
  161. #define DIR_SEPARATOR '/'
  162. #endif
  163.  
  164. static char dir_separator_str[] = {DIR_SEPARATOR, 0};
  165.  
  166. #define obstack_chunk_alloc xmalloc
  167. #define obstack_chunk_free free
  168.  
  169. extern void free ();
  170. extern char *getenv ();
  171.  
  172. #ifndef errno
  173. extern int errno;
  174. #endif
  175.  
  176. extern int sys_nerr;
  177. #ifndef HAVE_STRERROR
  178. #if defined(bsd4_4)
  179. extern const char *const sys_errlist[];
  180. #else
  181. extern char *sys_errlist[];
  182. #endif
  183. #else
  184. extern char *strerror();
  185. #endif
  186.  
  187. extern int execv (), execvp ();
  188.  
  189. /* If a stage of compilation returns an exit status >= 1,
  190.    compilation of that file ceases.  */
  191.  
  192. #define MIN_FATAL_STATUS 1
  193.  
  194. /* Flag saying to print the directories gcc will search through looking for
  195.    programs, libraries, etc.  */
  196.  
  197. static int print_search_dirs;
  198.  
  199. /* Flag saying to print the full filename of this file
  200.    as found through our usual search mechanism.  */
  201.  
  202. static char *print_file_name = NULL;
  203.  
  204. /* As print_file_name, but search for executable file. */
  205.  
  206. static char *print_prog_name = NULL;
  207.  
  208. /* Flag saying to print the relative path we'd use to
  209.    find libgcc.a given the current compiler flags.  */
  210.  
  211. static int print_multi_directory;
  212.  
  213. /* Flag saying to print the list of subdirectories and
  214.    compiler flags used to select them in a standard form.  */
  215.  
  216. static int print_multi_lib;
  217.  
  218. /* Flag indicating whether we should print the command and arguments */
  219.  
  220. static int verbose_flag;
  221.  
  222. /* Nonzero means write "temp" files in source directory
  223.    and use the source file's name in them, and don't delete them.  */
  224.  
  225. static int save_temps_flag;
  226.  
  227. #ifdef __amigados__
  228. /* Phil.B: 03-Oct-94 Flag indicating process priority */
  229. static int amiga_priority = 0;
  230. /* Phil.B: 17-Apr-95 Flag indicating stack checking */
  231. static int amiga_stackcheck = 0;
  232. #endif /* __amigados__ */
  233.  
  234. /* The compiler version.  */
  235.  
  236. static char *compiler_version;
  237.  
  238. /* The target version specified with -V */
  239.  
  240. static char *spec_version = DEFAULT_TARGET_VERSION;
  241.  
  242. /* The target machine specified with -b.  */
  243.  
  244. static char *spec_machine = DEFAULT_TARGET_MACHINE;
  245.  
  246. /* Nonzero if cross-compiling.
  247.    When -b is used, the value comes from the `specs' file.  */
  248.  
  249. #ifdef CROSS_COMPILE
  250. static int cross_compile = 1;
  251. #else
  252. static int cross_compile = 0;
  253. #endif
  254.  
  255. /* The number of errors that have occurred; the link phase will not be
  256.    run if this is non-zero.  */
  257. static int error_count = 0;
  258.  
  259. /* This is the obstack which we use to allocate many strings.  */
  260.  
  261. static struct obstack obstack;
  262.  
  263. /* This is the obstack to build an environment variable to pass to
  264.    collect2 that describes all of the relevant switches of what to
  265.    pass the compiler in building the list of pointers to constructors
  266.    and destructors.  */
  267.  
  268. static struct obstack collect_obstack;
  269.  
  270. extern char *version_string;
  271.  
  272. /* Forward declaration for prototypes.  */
  273. struct path_prefix;
  274.  
  275. static void set_spec        PROTO((char *, char *));
  276. static struct compiler *lookup_compiler PROTO((char *, int, char *));
  277. static char *build_search_list    PROTO((struct path_prefix *, char *, int));
  278. static void putenv_from_prefixes PROTO((struct path_prefix *, char *));
  279. static char *find_a_file    PROTO((struct path_prefix *, char *, int));
  280. static void add_prefix        PROTO((struct path_prefix *, char *, int, int, int *));
  281. static char *skip_whitespace    PROTO((char *));
  282. static void record_temp_file    PROTO((char *, int, int));
  283. static void delete_if_ordinary    PROTO((char *));
  284. static void delete_temp_files    PROTO((void));
  285. static void delete_failure_queue PROTO((void));
  286. static void clear_failure_queue PROTO((void));
  287. static char *choose_temp_base_try PROTO((char *, char *));
  288. static void choose_temp_base    PROTO((void));
  289. static int check_live_switch    PROTO((int, int));
  290. static char *handle_braces    PROTO((char *));
  291. static char *save_string    PROTO((char *, int));
  292. static char *concat        PROTO((char *, char *));
  293. static char *concat3        PROTO((char *, char *, char *));
  294. static char *concat4        PROTO((char *, char *, char *, char *));
  295. static char *concat6        PROTO((char *, char *, char *, char *, char *, \
  296.                                        char *));
  297. static int do_spec        PROTO((char *));
  298. static int do_spec_1        PROTO((char *, int, char *));
  299. static char *find_file        PROTO((char *));
  300. static int is_directory        PROTO((char *, char *, int));
  301. static void validate_switches    PROTO((char *));
  302. static void validate_all_switches PROTO((void));
  303. static void give_switch        PROTO((int, int));
  304. static int used_arg        PROTO((char *, int));
  305. static void set_multilib_dir    PROTO((void));
  306. static void print_multilib_info    PROTO((void));
  307. static void pfatal_with_name    PROTO((char *));
  308. static void perror_with_name    PROTO((char *));
  309. static void perror_exec        PROTO((char *));
  310. #ifdef HAVE_VPRINTF
  311. static void fatal        PVPROTO((char *, ...));
  312. static void error        PVPROTO((char *, ...));
  313. #else
  314. /* We must not provide any prototype here, even if ANSI C.  */
  315. static void fatal        PROTO(());
  316. static void error        PROTO(());
  317. #endif
  318.  
  319. void fancy_abort ();
  320. char *xmalloc ();
  321. char *xrealloc ();
  322.  
  323. /* Specs are strings containing lines, each of which (if not blank)
  324. is made up of a program name, and arguments separated by spaces.
  325. The program name must be exact and start from root, since no path
  326. is searched and it is unreliable to depend on the current working directory.
  327. Redirection of input or output is not supported; the subprograms must
  328. accept filenames saying what files to read and write.
  329.  
  330. In addition, the specs can contain %-sequences to substitute variable text
  331. or for conditional text.  Here is a table of all defined %-sequences.
  332. Note that spaces are not generated automatically around the results of
  333. expanding these sequences; therefore, you can concatenate them together
  334. or with constant text in a single argument.
  335.  
  336.  %%    substitute one % into the program name or argument.
  337.  %i     substitute the name of the input file being processed.
  338.  %b     substitute the basename of the input file being processed.
  339.     This is the substring up to (and not including) the last period
  340.     and not including the directory.
  341.  %g     substitute the temporary-file-name-base.  This is a string chosen
  342.     once per compilation.  Different temporary file names are made by
  343.     concatenation of constant strings on the end, as in `%g.s'.
  344.     %g also has the same effect of %d.
  345.  %u    like %g, but make the temporary file name unique.
  346.  %U    returns the last file name generated with %u.
  347.  %d    marks the argument containing or following the %d as a
  348.     temporary file name, so that that file will be deleted if CC exits
  349.     successfully.  Unlike %g, this contributes no text to the argument.
  350.  %w    marks the argument containing or following the %w as the
  351.     "output file" of this compilation.  This puts the argument
  352.     into the sequence of arguments that %o will substitute later.
  353.  %W{...}
  354.     like %{...} but mark last argument supplied within
  355.     as a file to be deleted on failure.
  356.  %o    substitutes the names of all the output files, with spaces
  357.     automatically placed around them.  You should write spaces
  358.     around the %o as well or the results are undefined.
  359.     %o is for use in the specs for running the linker.
  360.     Input files whose names have no recognized suffix are not compiled
  361.     at all, but they are included among the output files, so they will
  362.     be linked.
  363.  %O    substitutes the suffix for object files.
  364.  %p    substitutes the standard macro predefinitions for the
  365.     current target machine.  Use this when running cpp.
  366.  %P    like %p, but puts `__' before and after the name of each macro.
  367.     (Except macros that already have __.)
  368.     This is for ANSI C.
  369.  %I    Substitute a -iprefix option made from GCC_EXEC_PREFIX.
  370.  %s     current argument is the name of a library or startup file of some sort.
  371.         Search for that file in a standard list of directories
  372.     and substitute the full name found.
  373.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  374.         Use this when inconsistent options are detected.
  375.  %x{OPTION}    Accumulate an option for %X.
  376.  %X    Output the accumulated linker options specified by compilations.
  377.  %Y    Output the accumulated assembler options specified by compilations.
  378.  %Z    Output the accumulated preprocessor options specified by compilations.
  379.  %v1    Substitute the major version number of GCC.
  380.     (For version 2.5.n, this is 2.)
  381.  %v2    Substitute the minor version number of GCC.
  382.     (For version 2.5.n, this is 5.)
  383.  %a     process ASM_SPEC as a spec.
  384.         This allows config.h to specify part of the spec for running as.
  385.  %A    process ASM_FINAL_SPEC as a spec.  A capital A is actually
  386.     used here.  This can be used to run a post-processor after the
  387.     assembler has done it's job.
  388.  %D    Dump out a -L option for each directory in startfile_prefixes.
  389.     If multilib_dir is set, extra entries are generated with it affixed.
  390.  %l     process LINK_SPEC as a spec.
  391.  %L     process LIB_SPEC as a spec.
  392.  %G     process LIBGCC_SPEC as a spec.
  393.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  394.  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
  395.  %c    process SIGNED_CHAR_SPEC as a spec.
  396.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  397.  %1    process CC1_SPEC as a spec.
  398.  %2    process CC1PLUS_SPEC as a spec.
  399.  %|    output "-" if the input for the current command is coming from a pipe.
  400.  %*    substitute the variable part of a matched option.  (See below.)
  401.     Note that each comma in the substituted string is replaced by
  402.     a single space.
  403.  %{S}   substitutes the -S switch, if that switch was given to CC.
  404.     If that switch was not specified, this substitutes nothing.
  405.     Here S is a metasyntactic variable.
  406.  %{S*}  substitutes all the switches specified to CC whose names start
  407.     with -S.  This is used for -o, -D, -I, etc; switches that take
  408.     arguments.  CC considers `-o foo' as being one switch whose
  409.     name starts with `o'.  %{o*} would substitute this text,
  410.     including the space; thus, two arguments would be generated.
  411.  %{S*:X} substitutes X if one or more switches whose names start with -S are
  412.     specified to CC.  Note that the tail part of the -S option
  413.     (i.e. the part matched by the `*') will be substituted for each
  414.     occurrence of %* within X.
  415.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  416.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  417.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  418.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  419.  %{.S:X} substitutes X, but only if processing a file with suffix S.
  420.  %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
  421.  %(Spec) processes a specification defined in a specs file as *Spec:
  422.  %[Spec] as above, but put __ around -D arguments
  423.  
  424. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  425. other nested % constructs or spaces, or even newlines.  They are
  426. processed as usual, as described above.
  427.  
  428. The -O, -f, -m, and -W switches are handled specifically in these
  429. constructs.  If another value of -O or the negated form of a -f, -m, or
  430. -W switch is found later in the command line, the earlier switch
  431. value is ignored, except with {S*} where S is just one letter; this
  432. passes all matching options.
  433.  
  434. The character | is used to indicate that a command should be piped to
  435. the following command, but only if -pipe is specified.
  436.  
  437. Note that it is built into CC which switches take arguments and which
  438. do not.  You might think it would be useful to generalize this to
  439. allow each compiler's spec to say which switches take arguments.  But
  440. this cannot be done in a consistent fashion.  CC cannot even decide
  441. which input files have been specified without knowing which switches
  442. take arguments, and it must know which input files to compile in order
  443. to tell which compilers to run.
  444.  
  445. CC also knows implicitly that arguments starting in `-l' are to be
  446. treated as compiler output files, and passed to the linker in their
  447. proper position among the other output files.  */
  448.  
  449. /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1.  */
  450.  
  451. /* config.h can define ASM_SPEC to provide extra args to the assembler
  452.    or extra switch-translations.  */
  453. #ifndef ASM_SPEC
  454. #define ASM_SPEC ""
  455. #endif
  456.  
  457. /* config.h can define ASM_FINAL_SPEC to run a post processor after
  458.    the assembler has run.  */
  459. #ifndef ASM_FINAL_SPEC
  460. #define ASM_FINAL_SPEC ""
  461. #endif
  462.  
  463. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  464.    or extra switch-translations.  */
  465. #ifndef CPP_SPEC
  466. #define CPP_SPEC ""
  467. #endif
  468.  
  469. /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
  470.    or extra switch-translations.  */
  471. #ifndef CC1_SPEC
  472. #define CC1_SPEC ""
  473. #endif
  474.  
  475. /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
  476.    or extra switch-translations.  */
  477. #ifndef CC1PLUS_SPEC
  478. #define CC1PLUS_SPEC ""
  479. #endif
  480.  
  481. /* config.h can define LINK_SPEC to provide extra args to the linker
  482.    or extra switch-translations.  */
  483. #ifndef LINK_SPEC
  484. #define LINK_SPEC ""
  485. #endif
  486.  
  487. /* config.h can define LIB_SPEC to override the default libraries.  */
  488. #ifndef LIB_SPEC
  489. #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
  490. #endif
  491.  
  492. /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
  493.    included.  */
  494. #ifndef LIBGCC_SPEC
  495. #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
  496. /* Have gcc do the search for libgcc.a.  */
  497. #define LIBGCC_SPEC "%{!shared:libgcc.a%s}"
  498. #else
  499. #define LIBGCC_SPEC "%{!shared:-lgcc}"
  500. #endif
  501. #endif
  502.  
  503. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  504. #ifndef STARTFILE_SPEC
  505. #define STARTFILE_SPEC  \
  506.   "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
  507. #endif
  508.  
  509. /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
  510.    Make the string nonempty to require spaces there.  */
  511. #ifndef SWITCHES_NEED_SPACES
  512. #define SWITCHES_NEED_SPACES ""
  513. #endif
  514.  
  515. /* config.h can define ENDFILE_SPEC to override the default crtn files.  */
  516. #ifndef ENDFILE_SPEC
  517. #define ENDFILE_SPEC ""
  518. #endif
  519.  
  520. /* This spec is used for telling cpp whether char is signed or not.  */
  521. #ifndef SIGNED_CHAR_SPEC
  522. /* Use #if rather than ?:
  523.    because MIPS C compiler rejects like ?: in initializers.  */
  524. #if DEFAULT_SIGNED_CHAR
  525. #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
  526. #else
  527. #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
  528. #endif
  529. #endif
  530.  
  531. /* MULTILIB_SELECT comes from multilib.h.  It gives a
  532.    string interpreted by set_multilib_dir to select a library
  533.    subdirectory based on the compiler options.  */
  534. #ifndef MULTILIB_SELECT
  535. #define MULTILIB_SELECT ". ;"
  536. #endif
  537.  
  538. static char *cpp_spec = CPP_SPEC;
  539. static char *cpp_predefines = CPP_PREDEFINES;
  540. static char *cc1_spec = CC1_SPEC;
  541. static char *cc1plus_spec = CC1PLUS_SPEC;
  542. static char *signed_char_spec = SIGNED_CHAR_SPEC;
  543. static char *asm_spec = ASM_SPEC;
  544. static char *asm_final_spec = ASM_FINAL_SPEC;
  545. static char *link_spec = LINK_SPEC;
  546. static char *lib_spec = LIB_SPEC;
  547. static char *libgcc_spec = LIBGCC_SPEC;
  548. static char *endfile_spec = ENDFILE_SPEC;
  549. static char *startfile_spec = STARTFILE_SPEC;
  550. static char *switches_need_spaces = SWITCHES_NEED_SPACES;
  551. static char *multilib_select = MULTILIB_SELECT;
  552.  
  553. /* This defines which switch letters take arguments.  */
  554.  
  555. #ifndef SWITCH_TAKES_ARG
  556. #define SWITCH_TAKES_ARG(CHAR)      \
  557.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  558.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  559.    || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
  560.    || (CHAR) == 'L' || (CHAR) == 'A')
  561. #endif
  562.  
  563. /* This defines which multi-letter switches take arguments.  */
  564.  
  565. #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR)        \
  566.  (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext")    \
  567.   || !strcmp (STR, "Tbss") || !strcmp (STR, "include")    \
  568.   || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
  569.   || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
  570.   || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
  571.   || !strcmp (STR, "isystem"))
  572.  
  573. #ifndef WORD_SWITCH_TAKES_ARG
  574. #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
  575. #endif
  576.  
  577. /* Record the mapping from file suffixes for compilation specs.  */
  578.  
  579. struct compiler
  580. {
  581.   char *suffix;            /* Use this compiler for input files
  582.                    whose names end in this suffix.  */
  583.  
  584.   char *spec[4];        /* To use this compiler, concatenate these
  585.                    specs and pass to do_spec.  */
  586. };
  587.  
  588. /* Pointer to a vector of `struct compiler' that gives the spec for
  589.    compiling a file, based on its suffix.
  590.    A file that does not end in any of these suffixes will be passed
  591.    unchanged to the loader and nothing else will be done to it.
  592.  
  593.    An entry containing two 0s is used to terminate the vector.
  594.  
  595.    If multiple entries match a file, the last matching one is used.  */
  596.  
  597. static struct compiler *compilers;
  598.  
  599. /* Number of entries in `compilers', not counting the null terminator.  */
  600.  
  601. static int n_compilers;
  602.  
  603. /* The default list of file name suffixes and their compilation specs.  */
  604.  
  605. static struct compiler default_compilers[] =
  606. {
  607.   {".c", "@c"},
  608.   {"@c",
  609.    "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  610.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  611.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
  612.         -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  613.     %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  614.     %{!undef:%{!ansi:%p} %P} %{trigraphs} \
  615.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  616.         %{traditional-cpp:-traditional}\
  617.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
  618.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  619.    "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
  620.            %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
  621.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
  622.            %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
  623.            %{aux-info*}\
  624.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  625.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  626.               %{!S:as %a %Y\
  627.               %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
  628.                       %{!pipe:%g.s} %A\n }}}}"},
  629.   {"-",
  630.    "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  631.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  632.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
  633.         -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  634.     %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  635.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  636.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  637.         %{traditional-cpp:-traditional}\
  638.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
  639.         %i %W{o*}}\
  640.     %{!E:%e-E required when input is from standard input}"},
  641.   {".m", "@objective-c"},
  642.   {"@objective-c",
  643.    "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  644.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  645.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
  646.         -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  647.      %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  648.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  649.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  650.         %{traditional-cpp:-traditional}\
  651.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
  652.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  653.    "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
  654.            %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
  655.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
  656.            %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
  657.                -lang-objc %{gen-decls} \
  658.            %{aux-info*}\
  659.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  660.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  661.               %{!S:as %a %Y\
  662.               %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
  663.                       %{!pipe:%g.s} %A\n }}}}"},
  664.   {".h", "@c-header"},
  665.   {"@c-header",
  666.    "%{!E:%eCompilation of header file requested} \
  667.     cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  668.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  669.      %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
  670.         -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  671.      %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  672.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  673.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  674.         %{traditional-cpp:-traditional}\
  675.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
  676.         %i %W{o*}"},
  677.   {".i", "@cpp-output"},
  678.   {"@cpp-output",
  679.    "%{!M:%{!MM:%{!E:cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
  680.             %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi}\
  681.             %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
  682.             %{aux-info*}\
  683.             %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  684.             %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  685.              %{!S:as %a %Y\
  686.                  %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
  687.                  %{!pipe:%g.s} %A\n }}}}"},
  688.   {".s", "@assembler"},
  689.   {"@assembler",
  690.    "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
  691.                     %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
  692.                 %i %A\n }}}}"},
  693.   {".S", "@assembler-with-cpp"},
  694.   {"@assembler-with-cpp",
  695.    "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  696.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  697.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{trigraphs}\
  698.         -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
  699.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  700.         %{traditional-cpp:-traditional}\
  701.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
  702.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  703.    "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
  704.                     %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
  705.             %{!pipe:%g.s} %A\n }}}}"},
  706. #include "specs.h"
  707.   /* Mark end of table */
  708.   {0, 0}
  709. };
  710.  
  711. /* Number of elements in default_compilers, not counting the terminator.  */
  712.  
  713. static int n_default_compilers
  714.   = (sizeof default_compilers / sizeof (struct compiler)) - 1;
  715.  
  716. /* Here is the spec for running the linker, after compiling all files.  */
  717.  
  718. /* -u* was put back because both BSD and SysV seem to support it.  */
  719. /* %{static:} simply prevents an error message if the target machine
  720.    doesn't handle -static.  */
  721. /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
  722.    scripts which exist in user specified directories, or in standard
  723.    directories.  */
  724. #ifdef LINK_LIBGCC_SPECIAL
  725. /* Don't generate -L options.  */
  726. static char *link_command_spec = "\
  727. %{!fsyntax-only: \
  728.  %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  729.             %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
  730.             %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
  731.             %{static:} %{L*} %{T*} %o\
  732.             %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
  733.             %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\n }}}}}}";
  734. #else
  735. /* Use -L.  */
  736. static char *link_command_spec = "\
  737. %{!fsyntax-only: \
  738.  %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  739.             %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
  740.             %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
  741.             %{static:} %{L*} %D %{T*} %o\
  742.             %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
  743.             %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\n }}}}}}";
  744. #endif
  745.  
  746. /* A vector of options to give to the linker.
  747.    These options are accumulated by %x,
  748.    and substituted into the linker command with %X.  */
  749. static int n_linker_options;
  750. static char **linker_options;
  751.  
  752. /* A vector of options to give to the assembler.
  753.    These options are accumulated by -Wa,
  754.    and substituted into the assembler command with %Y.  */
  755. static int n_assembler_options;
  756. static char **assembler_options;
  757.  
  758. /* A vector of options to give to the preprocessor.
  759.    These options are accumulated by -Wp,
  760.    and substituted into the preprocessor command with %Z.  */
  761. static int n_preprocessor_options;
  762. static char **preprocessor_options;
  763.  
  764. /* Define how to map long options into short ones.  */
  765.  
  766. /* This structure describes one mapping.  */
  767. struct option_map
  768. {
  769.   /* The long option's name.  */
  770.   char *name;
  771.   /* The equivalent short option.  */
  772.   char *equivalent;
  773.   /* Argument info.  A string of flag chars; NULL equals no options.
  774.      a => argument required.
  775.      o => argument optional.
  776.      j => join argument to equivalent, making one word.
  777.      * => require other text after NAME as an argument.  */
  778.   char *arg_info;
  779. };
  780.  
  781. /* This is the table of mappings.  Mappings are tried sequentially
  782.    for each option encountered; the first one that matches, wins.  */
  783.  
  784. struct option_map option_map[] =
  785.  {
  786.    {"--all-warnings", "-Wall", 0},
  787.    {"--ansi", "-ansi", 0},
  788.    {"--assemble", "-S", 0},
  789.    {"--assert", "-A", "a"},
  790.    {"--comments", "-C", 0},
  791.    {"--compile", "-c", 0},
  792.    {"--debug", "-g", "oj"},
  793.    {"--define-macro", "-D", "a"},
  794.    {"--dependencies", "-M", 0},
  795.    {"--dump", "-d", "a"},
  796.    {"--dumpbase", "-dumpbase", "a"},
  797.    {"--entry", "-e", 0},
  798.    {"--extra-warnings", "-W", 0},
  799.    {"--for-assembler", "-Wa", "a"},
  800.    {"--for-linker", "-Xlinker", "a"},
  801.    {"--force-link", "-u", "a"},
  802.    {"--imacros", "-imacros", "a"},
  803.    {"--include", "-include", "a"},
  804.    {"--include-barrier", "-I-", 0},
  805.    {"--include-directory", "-I", "a"},
  806.    {"--include-directory-after", "-idirafter", "a"},
  807.    {"--include-prefix", "-iprefix", "a"},
  808.    {"--include-with-prefix", "-iwithprefix", "a"},
  809.    {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
  810.    {"--include-with-prefix-after", "-iwithprefix", "a"},
  811.    {"--language", "-x", "a"},
  812.    {"--library-directory", "-L", "a"},
  813.    {"--machine", "-m", "aj"},
  814.    {"--machine-", "-m", "*j"},
  815.    {"--no-line-commands", "-P", 0},
  816.    {"--no-precompiled-includes", "-noprecomp", 0},
  817.    {"--no-standard-includes", "-nostdinc", 0},
  818.    {"--no-standard-libraries", "-nostdlib", 0},
  819.    {"--no-warnings", "-w", 0},
  820.    {"--optimize", "-O", "oj"},
  821.    {"--output", "-o", "a"},
  822.    {"--pedantic", "-pedantic", 0},
  823.    {"--pedantic-errors", "-pedantic-errors", 0},
  824.    {"--pipe", "-pipe", 0},
  825.    {"--prefix", "-B", "a"},
  826. /* Phil.B: 03-Oct-94, allow priority settings for all programs started by gcc */
  827. #ifdef __amigados__
  828.    {"--priority", "-priority", "a"},
  829. #endif /* __amigados__ */
  830.    {"--preprocess", "-E", 0},
  831.    {"--print-search-dirs", "-print-search-dirs", 0},
  832.    {"--print-file-name", "-print-file-name=", "aj"},
  833.    {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
  834.    {"--print-missing-file-dependencies", "-MG", 0},
  835.    {"--print-multi-lib", "-print-multi-lib", 0},
  836.    {"--print-multi-directory", "-print-multi-directory", 0},
  837.    {"--print-prog-name", "-print-prog-name=", "aj"},
  838.    {"--profile", "-p", 0},
  839.    {"--profile-blocks", "-a", 0},
  840.    {"--quiet", "-q", 0},
  841.    {"--save-temps", "-save-temps", 0},
  842.    {"--shared", "-shared", 0},
  843.    {"--silent", "-q", 0},
  844.    {"--static", "-static", 0},
  845.    {"--symbolic", "-symbolic", 0},
  846.    {"--target", "-b", "a"},
  847.    {"--trace-includes", "-H", 0},
  848.    {"--traditional", "-traditional", 0},
  849.    {"--traditional-cpp", "-traditional-cpp", 0},
  850.    {"--trigraphs", "-trigraphs", 0},
  851.    {"--undefine-macro", "-U", "a"},
  852.    {"--use-version", "-V", "a"},
  853.    {"--user-dependencies", "-MM", 0},
  854.    {"--verbose", "-v", 0},
  855.    {"--version", "-dumpversion", 0},
  856.    {"--warn-", "-W", "*j"},
  857.    {"--write-dependencies", "-MD", 0},
  858.    {"--write-user-dependencies", "-MMD", 0},
  859.    {"--", "-f", "*j"}
  860.  };
  861.  
  862. /* Translate the options described by *ARGCP and *ARGVP.
  863.    Make a new vector and store it back in *ARGVP,
  864.    and store its length in *ARGVC.  */
  865.  
  866. static void
  867. translate_options (argcp, argvp)
  868.      int *argcp;
  869.      char ***argvp;
  870. {
  871.   int i, j, k;
  872.   int argc = *argcp;
  873.   char **argv = *argvp;
  874.   char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
  875.   int newindex = 0;
  876.  
  877.   i = 0;
  878.   newv[newindex++] = argv[i++];
  879.  
  880.   while (i < argc)
  881.     {
  882.       /* Translate -- options.  */
  883.       if (argv[i][0] == '-' && argv[i][1] == '-')
  884.     {
  885.       /* Find a mapping that applies to this option.  */
  886.       for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
  887.         {
  888.           int optlen = strlen (option_map[j].name);
  889.           int arglen = strlen (argv[i]);
  890.           int complen = arglen > optlen ? optlen : arglen;
  891.           char *arginfo = option_map[j].arg_info;
  892.  
  893.           if (arginfo == 0)
  894.         arginfo = "";
  895.  
  896.           if (!strncmp (argv[i], option_map[j].name, complen))
  897.         {
  898.           char *arg = 0;
  899.  
  900.           if (arglen < optlen)
  901.             {
  902.               for (k = j + 1;
  903.                k < sizeof (option_map) / sizeof (option_map[0]);
  904.                k++)
  905.             if (strlen (option_map[k].name) >= arglen
  906.                 && !strncmp (argv[i], option_map[k].name, arglen))
  907.               {
  908.                 error ("Ambiguous abbreviation %s", argv[i]);
  909.                 break;
  910.               }
  911.  
  912.               if (k != sizeof (option_map) / sizeof (option_map[0]))
  913.             break;
  914.             }
  915.  
  916.           if (arglen > optlen)
  917.             {
  918.               /* If the option has an argument, accept that.  */
  919.               if (argv[i][optlen] == '=')
  920.             arg = argv[i] + optlen + 1;
  921.  
  922.               /* If this mapping requires extra text at end of name,
  923.              accept that as "argument".  */
  924.               else if (index (arginfo, '*') != 0)
  925.             arg = argv[i] + optlen;
  926.  
  927.               /* Otherwise, extra text at end means mismatch.
  928.              Try other mappings.  */
  929.               else
  930.             continue;
  931.             }
  932.  
  933.           else if (index (arginfo, '*') != 0)
  934.             {
  935.               error ("Incomplete `%s' option", option_map[j].name);
  936.               break;
  937.             }
  938.  
  939.           /* Handle arguments.  */
  940.           if (index (arginfo, 'a') != 0)
  941.             {
  942.               if (arg == 0)
  943.             {
  944.               if (i + 1 == argc)
  945.                 {
  946.                   error ("Missing argument to `%s' option",
  947.                      option_map[j].name);
  948.                   break;
  949.                 }
  950.  
  951.               arg = argv[++i];
  952.             }
  953.             }
  954.           else if (index (arginfo, '*') != 0)
  955.             ;
  956.           else if (index (arginfo, 'o') == 0)
  957.             {
  958.               if (arg != 0)
  959.             error ("Extraneous argument to `%s' option",
  960.                    option_map[j].name);
  961.               arg = 0;
  962.             }
  963.  
  964.           /* Store the translation as one argv elt or as two.  */
  965.           if (arg != 0 && index (arginfo, 'j') != 0)
  966.             newv[newindex++] = concat (option_map[j].equivalent, arg);
  967.           else if (arg != 0)
  968.             {
  969.               newv[newindex++] = option_map[j].equivalent;
  970.               newv[newindex++] = arg;
  971.             }
  972.           else
  973.             newv[newindex++] = option_map[j].equivalent;
  974.  
  975.           break;
  976.         }
  977.         }
  978.       i++;
  979.     }
  980.  
  981.       /* Handle old-fashioned options--just copy them through,
  982.      with their arguments.  */
  983.       else if (argv[i][0] == '-')
  984.     {
  985.       char *p = argv[i] + 1;
  986.       int c = *p;
  987.       int nskip = 1;
  988.  
  989.       if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
  990.         nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
  991.       else if (WORD_SWITCH_TAKES_ARG (p))
  992.         nskip += WORD_SWITCH_TAKES_ARG (p);
  993.       else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
  994.            && p[1] == 0)
  995.         nskip += 1;
  996.       else if (! strcmp (p, "Xlinker"))
  997.         nskip += 1;
  998.  
  999.       /* Watch out for an option at the end of the command line that
  1000.          is missing arguments, and avoid skipping past the end of the
  1001.          command line.  */
  1002.       if (nskip + i > argc)
  1003.         nskip = argc - i;
  1004.  
  1005.       while (nskip > 0)
  1006.         {
  1007.           newv[newindex++] = argv[i++];
  1008.           nskip--;
  1009.         }
  1010.     }
  1011.       else
  1012.     /* Ordinary operands, or +e options.  */
  1013.     newv[newindex++] = argv[i++];
  1014.     }
  1015.  
  1016.   newv[newindex] = 0;
  1017.  
  1018.   *argvp = newv;
  1019.   *argcp = newindex;
  1020. }
  1021.  
  1022. char *
  1023. my_strerror(e)
  1024.      int e;
  1025. {
  1026.  
  1027. #ifdef HAVE_STRERROR
  1028.   return strerror(e);
  1029.  
  1030. #else
  1031.  
  1032.   static char buffer[30];
  1033.   if (!e)
  1034.     return "";
  1035.  
  1036.   if (e > 0 && e < sys_nerr)
  1037.     return sys_errlist[e];
  1038.  
  1039.   sprintf (buffer, "Unknown error %d", e);
  1040.   return buffer;
  1041. #endif
  1042. }
  1043.  
  1044. /* Read compilation specs from a file named FILENAME,
  1045.    replacing the default ones.
  1046.  
  1047.    A suffix which starts with `*' is a definition for
  1048.    one of the machine-specific sub-specs.  The "suffix" should be
  1049.    *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
  1050.    The corresponding spec is stored in asm_spec, etc.,
  1051.    rather than in the `compilers' vector.
  1052.  
  1053.    Anything invalid in the file is a fatal error.  */
  1054.  
  1055. static void
  1056. read_specs (filename)
  1057.      char *filename;
  1058. {
  1059.   int desc;
  1060.   int readlen;
  1061.   struct stat statbuf;
  1062.   char *buffer;
  1063.   register char *p;
  1064.  
  1065.   if (verbose_flag)
  1066.     fprintf (stderr, "Reading specs from %s\n", filename);
  1067.  
  1068.   /* Open and stat the file.  */
  1069.   desc = open (filename, O_RDONLY, 0);
  1070.   if (desc < 0)
  1071.     pfatal_with_name (filename);
  1072.   if (stat (filename, &statbuf) < 0)
  1073.     pfatal_with_name (filename);
  1074.  
  1075.   /* Read contents of file into BUFFER.  */
  1076.   buffer = xmalloc ((unsigned) statbuf.st_size + 1);
  1077.   readlen = read (desc, buffer, (unsigned) statbuf.st_size);
  1078.   if (readlen < 0)
  1079.     pfatal_with_name (filename);
  1080.   buffer[readlen] = 0;
  1081.   close (desc);
  1082.  
  1083.   /* Scan BUFFER for specs, putting them in the vector.  */
  1084.   p = buffer;
  1085.   while (1)
  1086.     {
  1087.       char *suffix;
  1088.       char *spec;
  1089.       char *in, *out, *p1, *p2;
  1090.  
  1091.       /* Advance P in BUFFER to the next nonblank nocomment line.  */
  1092.       p = skip_whitespace (p);
  1093.       if (*p == 0)
  1094.     break;
  1095.  
  1096.       /* Find the colon that should end the suffix.  */
  1097.       p1 = p;
  1098.       while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
  1099.       /* The colon shouldn't be missing.  */
  1100.       if (*p1 != ':')
  1101.     fatal ("specs file malformed after %d characters", p1 - buffer);
  1102.       /* Skip back over trailing whitespace.  */
  1103.       p2 = p1;
  1104.       while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
  1105.       /* Copy the suffix to a string.  */
  1106.       suffix = save_string (p, p2 - p);
  1107.       /* Find the next line.  */
  1108.       p = skip_whitespace (p1 + 1);
  1109.       if (p[1] == 0)
  1110.     fatal ("specs file malformed after %d characters", p - buffer);
  1111.       p1 = p;
  1112.       /* Find next blank line.  */
  1113.       while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
  1114.       /* Specs end at the blank line and do not include the newline.  */
  1115.       spec = save_string (p, p1 - p);
  1116.       p = p1;
  1117.  
  1118.       /* Delete backslash-newline sequences from the spec.  */
  1119.       in = spec;
  1120.       out = spec;
  1121.       while (*in != 0)
  1122.     {
  1123.       if (in[0] == '\\' && in[1] == '\n')
  1124.         in += 2;
  1125.       else if (in[0] == '#')
  1126.         {
  1127.           while (*in && *in != '\n') in++;
  1128.         }
  1129.       else
  1130.         *out++ = *in++;
  1131.     }
  1132.       *out = 0;
  1133.  
  1134.       if (suffix[0] == '*')
  1135.     {
  1136.       if (! strcmp (suffix, "*link_command"))
  1137.         link_command_spec = spec;
  1138.       else
  1139.         set_spec (suffix + 1, spec);
  1140.     }
  1141.       else
  1142.     {
  1143.       /* Add this pair to the vector.  */
  1144.       compilers
  1145.         = ((struct compiler *)
  1146.            xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
  1147.       compilers[n_compilers].suffix = suffix;
  1148.       bzero ((char *) compilers[n_compilers].spec,
  1149.          sizeof compilers[n_compilers].spec);
  1150.       compilers[n_compilers].spec[0] = spec;
  1151.       n_compilers++;
  1152.       bzero ((char *) &compilers[n_compilers],
  1153.          sizeof compilers[n_compilers]);
  1154.     }
  1155.  
  1156.       if (*suffix == 0)
  1157.     link_command_spec = spec;
  1158.     }
  1159.  
  1160.   if (link_command_spec == 0)
  1161.     fatal ("spec file has no spec for linking");
  1162. }
  1163.  
  1164. static char *
  1165. skip_whitespace (p)
  1166.      char *p;
  1167. {
  1168.   while (1)
  1169.     {
  1170.       /* A fully-blank line is a delimiter in the SPEC file and shouldn't
  1171.      be considered whitespace.  */
  1172.       if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
  1173.     return p + 1;
  1174.       else if (*p == '\n' || *p == ' ' || *p == '\t')
  1175.     p++;
  1176.       else if (*p == '#')
  1177.     {
  1178.       while (*p != '\n') p++;
  1179.       p++;
  1180.     }
  1181.       else
  1182.     break;
  1183.     }
  1184.  
  1185.   return p;
  1186. }
  1187.  
  1188. /* Structure to keep track of the specs that have been defined so far.  These
  1189.    are accessed using %(specname) or %[specname] in a compiler or link spec. */
  1190.  
  1191. struct spec_list
  1192. {
  1193.   char *name;                 /* Name of the spec. */
  1194.   char *spec;                 /* The spec itself. */
  1195.   struct spec_list *next;     /* Next spec in linked list. */
  1196. };
  1197.  
  1198. /* List of specs that have been defined so far. */
  1199.  
  1200. static struct spec_list *specs = (struct spec_list *) 0;
  1201.  
  1202. /* Change the value of spec NAME to SPEC.  If SPEC is empty, then the spec is
  1203.    removed; If the spec starts with a + then SPEC is added to the end of the
  1204.    current spec. */
  1205.  
  1206. static void
  1207. set_spec (name, spec)
  1208.      char *name;
  1209.      char *spec;
  1210. {
  1211.   struct spec_list *sl;
  1212.   char *old_spec;
  1213.  
  1214.   /* See if the spec already exists */
  1215.   for (sl = specs; sl; sl = sl->next)
  1216.     if (strcmp (sl->name, name) == 0)
  1217.       break;
  1218.  
  1219.   if (!sl)
  1220.     {
  1221.       /* Not found - make it */
  1222.       sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
  1223.       sl->name = save_string (name, strlen (name));
  1224.       sl->spec = save_string ("", 0);
  1225.       sl->next = specs;
  1226.       specs = sl;
  1227.     }
  1228.  
  1229.   old_spec = sl->spec;
  1230.   if (name && spec[0] == '+' && isspace (spec[1]))
  1231.     sl->spec = concat (old_spec, spec + 1);
  1232.   else
  1233.     sl->spec = save_string (spec, strlen (spec));
  1234.  
  1235.   if (! strcmp (name, "asm"))
  1236.     asm_spec = sl->spec;
  1237.   else if (! strcmp (name, "asm_final"))
  1238.     asm_final_spec = sl->spec;
  1239.   else if (! strcmp (name, "cc1"))
  1240.     cc1_spec = sl->spec;
  1241.   else if (! strcmp (name, "cc1plus"))
  1242.     cc1plus_spec = sl->spec;
  1243.   else if (! strcmp (name, "cpp"))
  1244.     cpp_spec = sl->spec;
  1245.   else if (! strcmp (name, "endfile"))
  1246.     endfile_spec = sl->spec;
  1247.   else if (! strcmp (name, "lib"))
  1248.     lib_spec = sl->spec;
  1249.   else if (! strcmp (name, "libgcc"))
  1250.     libgcc_spec = sl->spec;
  1251.   else if (! strcmp (name, "link"))
  1252.     link_spec = sl->spec;
  1253.   else if (! strcmp (name, "predefines"))
  1254.     cpp_predefines = sl->spec;
  1255.   else if (! strcmp (name, "signed_char"))
  1256.     signed_char_spec = sl->spec;
  1257.   else if (! strcmp (name, "startfile"))
  1258.     startfile_spec = sl->spec;
  1259.   else if (! strcmp (name, "switches_need_spaces"))
  1260.     switches_need_spaces = sl->spec;
  1261.   else if (! strcmp (name, "cross_compile"))
  1262.     cross_compile = atoi (sl->spec);
  1263.   else if (! strcmp (name, "multilib"))
  1264.     multilib_select = sl->spec;
  1265.   /* Free the old spec */
  1266.   if (old_spec)
  1267.     free (old_spec);
  1268. }
  1269.  
  1270. /* Accumulate a command (program name and args), and run it.  */
  1271.  
  1272. /* Vector of pointers to arguments in the current line of specifications.  */
  1273.  
  1274. static char **argbuf;
  1275.  
  1276. /* Number of elements allocated in argbuf.  */
  1277.  
  1278. static int argbuf_length;
  1279.  
  1280. /* Number of elements in argbuf currently in use (containing args).  */
  1281.  
  1282. static int argbuf_index;
  1283.  
  1284. /* This is the list of suffixes and codes (%g/%u/%U) and the associated
  1285.    temp file.  Used only if MKTEMP_EACH_FILE.  */
  1286.  
  1287. static struct temp_name {
  1288.   char *suffix;        /* suffix associated with the code.  */
  1289.   int length;        /* strlen (suffix).  */
  1290.   int unique;        /* Indicates whether %g or %u/%U was used.  */
  1291.   char *filename;    /* associated filename.  */
  1292.   int filename_length;    /* strlen (filename).  */
  1293.   struct temp_name *next;
  1294. } *temp_names;
  1295.  
  1296. /* Number of commands executed so far.  */
  1297.  
  1298. static int execution_count;
  1299.  
  1300. /* Number of commands that exited with a signal.  */
  1301.  
  1302. static int signal_count;
  1303.  
  1304. /* Name with which this program was invoked.  */
  1305.  
  1306. static char *programname;
  1307.  
  1308. /* Structures to keep track of prefixes to try when looking for files. */
  1309.  
  1310. struct prefix_list
  1311. {
  1312.   char *prefix;               /* String to prepend to the path. */
  1313.   struct prefix_list *next;   /* Next in linked list. */
  1314.   int require_machine_suffix; /* Don't use without machine_suffix.  */
  1315.   /* 2 means try both machine_suffix and just_machine_suffix.  */
  1316.   int *used_flag_ptr;          /* 1 if a file was found with this prefix.  */
  1317. };
  1318.  
  1319. struct path_prefix
  1320. {
  1321.   struct prefix_list *plist;  /* List of prefixes to try */
  1322.   int max_len;                /* Max length of a prefix in PLIST */
  1323.   char *name;                 /* Name of this list (used in config stuff) */
  1324. };
  1325.  
  1326. /* List of prefixes to try when looking for executables. */
  1327.  
  1328. static struct path_prefix exec_prefixes = { 0, 0, "exec" };
  1329.  
  1330. /* List of prefixes to try when looking for startup (crt0) files. */
  1331.  
  1332. static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
  1333.  
  1334. /* List of prefixes to try when looking for include files.  */
  1335.  
  1336. static struct path_prefix include_prefixes = { 0, 0, "include" };
  1337.  
  1338. /* Suffix to attach to directories searched for commands.
  1339.    This looks like `MACHINE/VERSION/'.  */
  1340.  
  1341. static char *machine_suffix = 0;
  1342.  
  1343. /* Suffix to attach to directories searched for commands.
  1344.    This is just `MACHINE/'.  */
  1345.  
  1346. static char *just_machine_suffix = 0;
  1347.  
  1348. /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
  1349.  
  1350. static char *gcc_exec_prefix;
  1351.  
  1352. /* Default prefixes to attach to command names.  */
  1353.  
  1354. #ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
  1355. #undef MD_EXEC_PREFIX
  1356. #undef MD_STARTFILE_PREFIX
  1357. #undef MD_STARTFILE_PREFIX_1
  1358. #endif
  1359.  
  1360. #ifndef STANDARD_EXEC_PREFIX
  1361. #define STANDARD_EXEC_PREFIX "/gnu/lib/gcc-lib/"
  1362. #endif /* !defined STANDARD_EXEC_PREFIX */
  1363.  
  1364. static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  1365. static char *standard_exec_prefix_1 = "/local/lib/gcc-lib/";
  1366. #ifdef MD_EXEC_PREFIX
  1367. static char *md_exec_prefix = MD_EXEC_PREFIX;
  1368. #endif
  1369.  
  1370. #ifndef STANDARD_STARTFILE_PREFIX
  1371. #define STANDARD_STARTFILE_PREFIX "/gnu/lib/"
  1372. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  1373.  
  1374. #ifdef MD_STARTFILE_PREFIX
  1375. static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
  1376. #endif
  1377. #ifdef MD_STARTFILE_PREFIX_1
  1378. static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
  1379. #endif
  1380. static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  1381. static char *standard_startfile_prefix_1 = "/local/lib/";
  1382. static char *standard_startfile_prefix_2 = "/local/lib/";
  1383.  
  1384. #ifndef TOOLDIR_BASE_PREFIX
  1385. #define TOOLDIR_BASE_PREFIX "/local/"
  1386. #endif
  1387. static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
  1388. static char *tooldir_prefix;
  1389.  
  1390. /* Subdirectory to use for locating libraries.  Set by
  1391.    set_multilib_dir based on the compilation options.  */
  1392.  
  1393. static char *multilib_dir;
  1394.  
  1395. /* Clear out the vector of arguments (after a command is executed).  */
  1396.  
  1397. static void
  1398. clear_args ()
  1399. {
  1400.   argbuf_index = 0;
  1401. }
  1402.  
  1403. /* Add one argument to the vector at the end.
  1404.    This is done when a space is seen or at the end of the line.
  1405.    If DELETE_ALWAYS is nonzero, the arg is a filename
  1406.     and the file should be deleted eventually.
  1407.    If DELETE_FAILURE is nonzero, the arg is a filename
  1408.     and the file should be deleted if this compilation fails.  */
  1409.  
  1410. static void
  1411. store_arg (arg, delete_always, delete_failure)
  1412.      char *arg;
  1413.      int delete_always, delete_failure;
  1414. {
  1415.   if (argbuf_index + 1 == argbuf_length)
  1416.     {
  1417.       argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  1418.     }
  1419.  
  1420.   argbuf[argbuf_index++] = arg;
  1421.   argbuf[argbuf_index] = 0;
  1422.  
  1423.   if (delete_always || delete_failure)
  1424.     record_temp_file (arg, delete_always, delete_failure);
  1425. }
  1426.  
  1427. /* Record the names of temporary files we tell compilers to write,
  1428.    and delete them at the end of the run.  */
  1429.  
  1430. /* This is the common prefix we use to make temp file names.
  1431.    It is chosen once for each run of this program.
  1432.    It is substituted into a spec by %g.
  1433.    Thus, all temp file names contain this prefix.
  1434.    In practice, all temp file names start with this prefix.
  1435.  
  1436.    This prefix comes from the envvar TMPDIR if it is defined;
  1437.    otherwise, from the P_tmpdir macro if that is defined;
  1438.    otherwise, in /usr/tmp or /tmp.  */
  1439.  
  1440. static char *temp_filename;
  1441.  
  1442. /* Length of the prefix.  */
  1443.  
  1444. static int temp_filename_length;
  1445.  
  1446. /* Define the list of temporary files to delete.  */
  1447.  
  1448. struct temp_file
  1449. {
  1450.   char *name;
  1451.   struct temp_file *next;
  1452. };
  1453.  
  1454. /* Queue of files to delete on success or failure of compilation.  */
  1455. static struct temp_file *always_delete_queue;
  1456. /* Queue of files to delete on failure of compilation.  */
  1457. static struct temp_file *failure_delete_queue;
  1458.  
  1459. /* Record FILENAME as a file to be deleted automatically.
  1460.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  1461.    otherwise delete it in any case.
  1462.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  1463.    otherwise delete it in any case.  */
  1464.  
  1465. static void
  1466. record_temp_file (filename, always_delete, fail_delete)
  1467.      char *filename;
  1468.      int always_delete;
  1469.      int fail_delete;
  1470. {
  1471.   register char *name;
  1472.   name = xmalloc (strlen (filename) + 1);
  1473.   strcpy (name, filename);
  1474.  
  1475.   if (always_delete)
  1476.     {
  1477.       register struct temp_file *temp;
  1478.       for (temp = always_delete_queue; temp; temp = temp->next)
  1479.     if (! strcmp (name, temp->name))
  1480.       goto already1;
  1481.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  1482.       temp->next = always_delete_queue;
  1483.       temp->name = name;
  1484.       always_delete_queue = temp;
  1485.     already1:;
  1486.     }
  1487.  
  1488.   if (fail_delete)
  1489.     {
  1490.       register struct temp_file *temp;
  1491.       for (temp = failure_delete_queue; temp; temp = temp->next)
  1492.     if (! strcmp (name, temp->name))
  1493.       goto already2;
  1494.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  1495.       temp->next = failure_delete_queue;
  1496.       temp->name = name;
  1497.       failure_delete_queue = temp;
  1498.     already2:;
  1499.     }
  1500. }
  1501.  
  1502. /* Delete all the temporary files whose names we previously recorded.  */
  1503.  
  1504. static void
  1505. delete_if_ordinary (name)
  1506.      char *name;
  1507. {
  1508.   struct stat st;
  1509. #ifdef DEBUG
  1510.   int i, c;
  1511.  
  1512.   printf ("Delete %s? (y or n) ", name);
  1513.   fflush (stdout);
  1514.   i = getchar ();
  1515.   if (i != '\n')
  1516.     while ((c = getchar ()) != '\n' && c != EOF) ;
  1517.   if (i == 'y' || i == 'Y')
  1518. #endif /* DEBUG */
  1519.     if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
  1520.       if (unlink (name) < 0)
  1521.     if (verbose_flag)
  1522.       perror_with_name (name);
  1523. }
  1524.  
  1525. static void
  1526. delete_temp_files ()
  1527. {
  1528.   register struct temp_file *temp;
  1529.  
  1530.   for (temp = always_delete_queue; temp; temp = temp->next)
  1531.     delete_if_ordinary (temp->name);
  1532.   always_delete_queue = 0;
  1533. }
  1534.  
  1535. /* Delete all the files to be deleted on error.  */
  1536.  
  1537. static void
  1538. delete_failure_queue ()
  1539. {
  1540.   register struct temp_file *temp;
  1541.  
  1542.   for (temp = failure_delete_queue; temp; temp = temp->next)
  1543.     delete_if_ordinary (temp->name);
  1544. }
  1545.  
  1546. static void
  1547. clear_failure_queue ()
  1548. {
  1549.   failure_delete_queue = 0;
  1550. }
  1551.  
  1552. /* Compute a string to use as the base of all temporary file names.
  1553.    It is substituted for %g.  */
  1554.  
  1555. static char *
  1556. choose_temp_base_try (try, base)
  1557.      char *try;
  1558.      char *base;
  1559. {
  1560.   char *rv;
  1561.   if (base)
  1562.     rv = base;
  1563.   else if (try == (char *)0)
  1564.     rv = 0;
  1565.   else if (access (try, R_OK | W_OK) != 0)
  1566.     rv = 0;
  1567.   else
  1568.     rv = try;
  1569.   return rv;
  1570. }
  1571.  
  1572. static void
  1573. choose_temp_base ()
  1574. {
  1575.   char *base = 0;
  1576.   int len;
  1577.  
  1578.   base = choose_temp_base_try (getenv ("TMPDIR"), base);
  1579.   base = choose_temp_base_try (getenv ("TMP"), base);
  1580.   base = choose_temp_base_try (getenv ("TEMP"), base);
  1581.  
  1582. #ifdef P_tmpdir
  1583.   base = choose_temp_base_try (P_tmpdir, base);
  1584. #endif
  1585.  
  1586. #ifdef __amigados__
  1587.   if (!base) /* No env var set */
  1588.     base = "RAM:";
  1589. #else
  1590.   base = choose_temp_base_try (concat4 (dir_separator_str, "usr", 
  1591.                                         dir_separator_str, "tmp"), 
  1592.                                 base);
  1593.   base = choose_temp_base_try (concat (dir_separator_str, "tmp"), base);
  1594.  
  1595. #endif
  1596.  
  1597.   /* If all else fails, use the current directory! */  
  1598.   if (base == (char *)0) base = concat(".", dir_separator_str);
  1599.  
  1600.   len = strlen (base);
  1601.   temp_filename = xmalloc (len + strlen (concat (dir_separator_str, 
  1602.                                                  "ccXXXXXX")) + 1);
  1603.   strcpy (temp_filename, base);
  1604.   if (len > 0 && temp_filename[len-1] != '/'
  1605.       && temp_filename[len-1] != DIR_SEPARATOR
  1606. #ifdef VOL_SEPARATOR
  1607.       && temp_filename[len-1] != VOL_SEPARATOR
  1608. #endif
  1609.     )
  1610.     temp_filename[len++] = DIR_SEPARATOR;
  1611.   strcpy (temp_filename + len, "ccXXXXXX");
  1612.  
  1613.   mktemp (temp_filename);
  1614.   temp_filename_length = strlen (temp_filename);
  1615.   if (temp_filename_length == 0)
  1616.     abort ();
  1617. }
  1618.  
  1619.  
  1620. /* Routine to add variables to the environment.  We do this to pass
  1621.    the pathname of the gcc driver, and the directories search to the
  1622.    collect2 program, which is being run as ld.  This way, we can be
  1623.    sure of executing the right compiler when collect2 wants to build
  1624.    constructors and destructors.  Since the environment variables we
  1625.    use come from an obstack, we don't have to worry about allocating
  1626.    space for them.  */
  1627.  
  1628. #ifndef HAVE_PUTENV
  1629.  
  1630. void
  1631. putenv (str)
  1632.      char *str;
  1633. {
  1634. #ifndef VMS            /* nor about VMS */
  1635.  
  1636.   extern char **environ;
  1637.   char **old_environ = environ;
  1638.   char **envp;
  1639.   int num_envs = 0;
  1640.   int name_len = 1;
  1641.   int str_len = strlen (str);
  1642.   char *p = str;
  1643.   int ch;
  1644.  
  1645.   while ((ch = *p++) != '\0' && ch != '=')
  1646.     name_len++;
  1647.  
  1648.   if (!ch)
  1649.     abort ();
  1650.  
  1651.   /* Search for replacing an existing environment variable, and
  1652.      count the number of total environment variables.  */
  1653.   for (envp = old_environ; *envp; envp++)
  1654.     {
  1655.       num_envs++;
  1656.       if (!strncmp (str, *envp, name_len))
  1657.     {
  1658.       *envp = str;
  1659.       return;
  1660.     }
  1661.     }
  1662.  
  1663.   /* Add a new environment variable */
  1664.   environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
  1665.   *environ = str;
  1666.   bcopy ((char *) old_environ, (char *) (environ + 1),
  1667.      sizeof (char *) * (num_envs+1));
  1668.  
  1669. #endif    /* VMS */
  1670. }
  1671.  
  1672. #endif    /* HAVE_PUTENV */
  1673.  
  1674.  
  1675. /* Build a list of search directories from PATHS.
  1676.    PREFIX is a string to prepend to the list.
  1677.    If CHECK_DIR_P is non-zero we ensure the directory exists.
  1678.    This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
  1679.    It is also used by the --print-search-dirs flag.  */
  1680.  
  1681. static char *
  1682. build_search_list (paths, prefix, check_dir_p)
  1683.      struct path_prefix *paths;
  1684.      char *prefix;
  1685.      int check_dir_p;
  1686. {
  1687.   int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
  1688.   int just_suffix_len
  1689.     = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
  1690.   int first_time = TRUE;
  1691.   struct prefix_list *pprefix;
  1692.  
  1693.   obstack_grow (&collect_obstack, prefix, strlen (prefix));
  1694.  
  1695.   for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
  1696.     {
  1697.       int len = strlen (pprefix->prefix);
  1698.  
  1699.       if (machine_suffix
  1700.       && (!check_dir_p
  1701.           || is_directory (pprefix->prefix, machine_suffix, 0)))
  1702.     {
  1703.       if (!first_time)
  1704.         obstack_1grow (&collect_obstack, PATH_SEPARATOR);
  1705.         
  1706.       first_time = FALSE;
  1707.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1708.       obstack_grow (&collect_obstack, machine_suffix, suffix_len);
  1709.     }
  1710.  
  1711.       if (just_machine_suffix
  1712.       && pprefix->require_machine_suffix == 2
  1713.       && (!check_dir_p
  1714.           || is_directory (pprefix->prefix, just_machine_suffix, 0)))
  1715.     {
  1716.       if (!first_time)
  1717.         obstack_1grow (&collect_obstack, PATH_SEPARATOR);
  1718.         
  1719.       first_time = FALSE;
  1720.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1721.       obstack_grow (&collect_obstack, just_machine_suffix,
  1722.             just_suffix_len);
  1723.     }
  1724.  
  1725.       if (!pprefix->require_machine_suffix)
  1726.     {
  1727.       if (!first_time)
  1728.         obstack_1grow (&collect_obstack, PATH_SEPARATOR);
  1729.  
  1730.       first_time = FALSE;
  1731.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1732.     }
  1733.     }
  1734.   obstack_1grow (&collect_obstack, '\0');
  1735.   return obstack_finish (&collect_obstack);
  1736. }
  1737.  
  1738. /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect.  */
  1739.  
  1740. static void
  1741. putenv_from_prefixes (paths, env_var)
  1742.      struct path_prefix *paths;
  1743.      char *env_var;
  1744. {
  1745.   putenv (build_search_list (paths, env_var, 1));
  1746. }
  1747.  
  1748. /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
  1749.    access to check permissions.
  1750.    Return 0 if not found, otherwise return its name, allocated with malloc. */
  1751.  
  1752. static char *
  1753. find_a_file (pprefix, name, mode)
  1754.      struct path_prefix *pprefix;
  1755.      char *name;
  1756.      int mode;
  1757. {
  1758.   char *temp;
  1759.   char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
  1760.   struct prefix_list *pl;
  1761.   int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
  1762.  
  1763.   if (machine_suffix)
  1764.     len += strlen (machine_suffix);
  1765.  
  1766.   temp = xmalloc (len);
  1767.  
  1768.   /* Determine the filename to execute (special case for absolute paths).  */
  1769.  
  1770.   if (*name == '/' || *name == DIR_SEPARATOR
  1771. #ifdef VOL_SEPARATOR
  1772.       || index (name, VOL_SEPARATOR)
  1773. #endif
  1774.     )
  1775.     {
  1776.       if (access (name, mode))
  1777.     {
  1778.       strcpy (temp, name);
  1779.       return temp;
  1780.     }
  1781.     }
  1782.   else
  1783.     for (pl = pprefix->plist; pl; pl = pl->next)
  1784.       {
  1785.     if (machine_suffix)
  1786.       {
  1787.         /* Some systems have a suffix for executable files.
  1788.            So try appending that first.  */
  1789.         if (file_suffix[0] != 0)
  1790.           {
  1791.         strcpy (temp, pl->prefix);
  1792.         strcat (temp, machine_suffix);
  1793.         strcat (temp, name);
  1794.         strcat (temp, file_suffix);
  1795.         if (access (temp, mode) == 0)
  1796.           {
  1797.             if (pl->used_flag_ptr != 0)
  1798.               *pl->used_flag_ptr = 1;
  1799.             return temp;
  1800.           }
  1801.           }
  1802.  
  1803.         /* Now try just the name.  */
  1804.         strcpy (temp, pl->prefix);
  1805.         strcat (temp, machine_suffix);
  1806.         strcat (temp, name);
  1807.         if (access (temp, mode) == 0)
  1808.           {
  1809.         if (pl->used_flag_ptr != 0)
  1810.           *pl->used_flag_ptr = 1;
  1811.         return temp;
  1812.           }
  1813.       }
  1814.  
  1815.     /* Certain prefixes are tried with just the machine type,
  1816.        not the version.  This is used for finding as, ld, etc.  */
  1817.     if (just_machine_suffix && pl->require_machine_suffix == 2)
  1818.       {
  1819.         /* Some systems have a suffix for executable files.
  1820.            So try appending that first.  */
  1821.         if (file_suffix[0] != 0)
  1822.           {
  1823.         strcpy (temp, pl->prefix);
  1824.         strcat (temp, just_machine_suffix);
  1825.         strcat (temp, name);
  1826.         strcat (temp, file_suffix);
  1827.         if (access (temp, mode) == 0)
  1828.           {
  1829.             if (pl->used_flag_ptr != 0)
  1830.               *pl->used_flag_ptr = 1;
  1831.             return temp;
  1832.           }
  1833.           }
  1834.  
  1835.         strcpy (temp, pl->prefix);
  1836.         strcat (temp, just_machine_suffix);
  1837.         strcat (temp, name);
  1838.         if (access (temp, mode) == 0)
  1839.           {
  1840.         if (pl->used_flag_ptr != 0)
  1841.           *pl->used_flag_ptr = 1;
  1842.         return temp;
  1843.           }
  1844.       }
  1845.  
  1846.     /* Certain prefixes can't be used without the machine suffix
  1847.        when the machine or version is explicitly specified.  */
  1848.     if (!pl->require_machine_suffix)
  1849.       {
  1850.         /* Some systems have a suffix for executable files.
  1851.            So try appending that first.  */
  1852.         if (file_suffix[0] != 0)
  1853.           {
  1854.         strcpy (temp, pl->prefix);
  1855.         strcat (temp, name);
  1856.         strcat (temp, file_suffix);
  1857.         if (access (temp, mode) == 0)
  1858.           {
  1859.             if (pl->used_flag_ptr != 0)
  1860.               *pl->used_flag_ptr = 1;
  1861.             return temp;
  1862.           }
  1863.           }
  1864.  
  1865.         strcpy (temp, pl->prefix);
  1866.         strcat (temp, name);
  1867.         if (access (temp, mode) == 0)
  1868.           {
  1869.         if (pl->used_flag_ptr != 0)
  1870.           *pl->used_flag_ptr = 1;
  1871.         return temp;
  1872.           }
  1873.       }
  1874.       }
  1875.  
  1876.   free (temp);
  1877.   return 0;
  1878. }
  1879.  
  1880. /* Add an entry for PREFIX in PLIST.  If FIRST is set, it goes
  1881.    at the start of the list, otherwise it goes at the end.
  1882.  
  1883.    If WARN is nonzero, we will warn if no file is found
  1884.    through this prefix.  WARN should point to an int
  1885.    which will be set to 1 if this entry is used.
  1886.  
  1887.    REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
  1888.    the complete value of machine_suffix.
  1889.    2 means try both machine_suffix and just_machine_suffix.  */
  1890.  
  1891. static void
  1892. add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
  1893.      struct path_prefix *pprefix;
  1894.      char *prefix;
  1895.      int first;
  1896.      int require_machine_suffix;
  1897.      int *warn;
  1898. {
  1899.   struct prefix_list *pl, **prev;
  1900.   int len;
  1901.  
  1902.   if (!first && pprefix->plist)
  1903.     {
  1904.       for (pl = pprefix->plist; pl->next; pl = pl->next)
  1905.     ;
  1906.       prev = &pl->next;
  1907.     }
  1908.   else
  1909.     prev = &pprefix->plist;
  1910.  
  1911.   /* Keep track of the longest prefix */
  1912.  
  1913.   len = strlen (prefix);
  1914.   if (len > pprefix->max_len)
  1915.     pprefix->max_len = len;
  1916.  
  1917.   pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
  1918.   pl->prefix = save_string (prefix, len);
  1919.   pl->require_machine_suffix = require_machine_suffix;
  1920.   pl->used_flag_ptr = warn;
  1921.   if (warn)
  1922.     *warn = 0;
  1923.  
  1924.   if (*prev)
  1925.     pl->next = *prev;
  1926.   else
  1927.     pl->next = (struct prefix_list *) 0;
  1928.   *prev = pl;
  1929. }
  1930.  
  1931. /* Print warnings for any prefixes in the list PPREFIX that were not used.  */
  1932.  
  1933. static void
  1934. unused_prefix_warnings (pprefix)
  1935.      struct path_prefix *pprefix;
  1936. {
  1937.   struct prefix_list *pl = pprefix->plist;
  1938.  
  1939.   while (pl)
  1940.     {
  1941.       if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
  1942.     {
  1943.       error ("file path prefix `%s' never used",
  1944.          pl->prefix);
  1945.       /* Prevent duplicate warnings.  */
  1946.       *pl->used_flag_ptr = 1;
  1947.     }
  1948.       pl = pl->next;
  1949.     }
  1950. }
  1951.  
  1952. /* Get rid of all prefixes built up so far in *PLISTP. */
  1953.  
  1954. static void
  1955. free_path_prefix (pprefix)
  1956.      struct path_prefix *pprefix;
  1957. {
  1958.   struct prefix_list *pl = pprefix->plist;
  1959.   struct prefix_list *temp;
  1960.  
  1961.   while (pl)
  1962.     {
  1963.       temp = pl;
  1964.       pl = pl->next;
  1965.       free (temp->prefix);
  1966.       free ((char *) temp);
  1967.     }
  1968.   pprefix->plist = (struct prefix_list *) 0;
  1969. }
  1970.  
  1971. /* stdin file number.  */
  1972. #define STDIN_FILE_NO 0
  1973.  
  1974. /* stdout file number.  */
  1975. #define STDOUT_FILE_NO 1
  1976.  
  1977. /* value of `pipe': port index for reading.  */
  1978. #define READ_PORT 0
  1979.  
  1980. /* value of `pipe': port index for writing.  */
  1981. #define WRITE_PORT 1
  1982.  
  1983. /* Pipe waiting from last process, to be used as input for the next one.
  1984.    Value is STDIN_FILE_NO if no pipe is waiting
  1985.    (i.e. the next command is the first of a group).  */
  1986.  
  1987. static int last_pipe_input;
  1988.  
  1989. /* Fork one piped subcommand.  FUNC is the system call to use
  1990.    (either execv or execvp).  ARGV is the arg vector to use.
  1991.    NOT_LAST is nonzero if this is not the last subcommand
  1992.    (i.e. its output should be piped to the next one.)  */
  1993.  
  1994. #ifndef PEXECUTE
  1995. #ifdef __MSDOS__
  1996.  
  1997. #include <process.h>
  1998. static int
  1999. pexecute (search_flag, program, argv, not_last)
  2000.      int search_flag;
  2001.      char *program;
  2002.      char *argv[];
  2003.      int not_last;
  2004. {
  2005. #ifdef __GO32__
  2006.   int i = (search_flag ? spawnv : spawnvp) (1, program, argv);
  2007. #else
  2008.   char *scmd, *rf;
  2009.   FILE *argfile;
  2010.   int i, el = search_flag ? 0 : 4;
  2011.  
  2012.   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6 + el);
  2013.   rf = scmd + strlen(program) + 2 + el;
  2014.   sprintf (scmd, "%s%s @%s.gp", program,
  2015.        (search_flag ? "" : ".exe"), temp_filename);
  2016.   argfile = fopen (rf, "w");
  2017.   if (argfile == 0)
  2018.     pfatal_with_name (rf);
  2019.  
  2020.   for (i=1; argv[i]; i++)
  2021.     {
  2022.       char *cp;
  2023.       for (cp = argv[i]; *cp; cp++)
  2024.     {
  2025.       if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
  2026.         fputc ('\\', argfile);
  2027.       fputc (*cp, argfile);
  2028.     }
  2029.       fputc ('\n', argfile);
  2030.     }
  2031.   fclose (argfile);
  2032.  
  2033.   i = system (scmd);
  2034.  
  2035.   remove (rf);
  2036. #endif
  2037.   
  2038.   if (i == -1)
  2039.     {
  2040.       perror_exec (program);
  2041.       return MIN_FATAL_STATUS << 8;
  2042.     }
  2043.   return i << 8;
  2044. }
  2045.  
  2046. #endif
  2047.  
  2048. #if !defined(__MSDOS__) && !defined(OS2) && !defined(_WIN32)
  2049.  
  2050. static int
  2051. pexecute (search_flag, program, argv, not_last)
  2052.      int search_flag;
  2053.      char *program;
  2054.      char *argv[];
  2055.      int not_last;
  2056. {
  2057.   int (*func)() = (search_flag ? execv : execvp);
  2058.   int pid;
  2059.   int pdes[2];
  2060.   int input_desc = last_pipe_input;
  2061.   int output_desc = STDOUT_FILE_NO;
  2062.   int retries, sleep_interval;
  2063.  
  2064.   /* If this isn't the last process, make a pipe for its output,
  2065.      and record it as waiting to be the input to the next process.  */
  2066.  
  2067.   if (not_last)
  2068.     {
  2069.       if (pipe (pdes) < 0)
  2070.     pfatal_with_name ("pipe");
  2071.       output_desc = pdes[WRITE_PORT];
  2072.       last_pipe_input = pdes[READ_PORT];
  2073.     }
  2074.   else
  2075.     last_pipe_input = STDIN_FILE_NO;
  2076.  
  2077.   /* Fork a subprocess; wait and retry if it fails.  */
  2078.   sleep_interval = 1;
  2079.   for (retries = 0; retries < 4; retries++)
  2080.     {
  2081.       pid = vfork ();
  2082.       if (pid >= 0)
  2083.     break;
  2084.       sleep (sleep_interval);
  2085.       sleep_interval *= 2;
  2086.     }
  2087.  
  2088.   switch (pid)
  2089.     {
  2090.     case -1:
  2091. #ifdef vfork
  2092.       pfatal_with_name ("fork");
  2093. #else
  2094.       pfatal_with_name ("vfork");
  2095. #endif
  2096.       /* NOTREACHED */
  2097.       return 0;
  2098.  
  2099.     case 0: /* child */
  2100.       /* Move the input and output pipes into place, if nec.  */
  2101.       if (input_desc != STDIN_FILE_NO)
  2102.     {
  2103.       close (STDIN_FILE_NO);
  2104.       dup (input_desc);
  2105.       close (input_desc);
  2106.     }
  2107.       if (output_desc != STDOUT_FILE_NO)
  2108.     {
  2109.       close (STDOUT_FILE_NO);
  2110.       dup (output_desc);
  2111.       close (output_desc);
  2112.     }
  2113.  
  2114.       /* Close the parent's descs that aren't wanted here.  */
  2115.       if (last_pipe_input != STDIN_FILE_NO)
  2116.     close (last_pipe_input);
  2117.  
  2118.       /* Exec the program.  */
  2119.       (*func) (program, argv);
  2120.       perror_exec (program);
  2121.       exit (-1);
  2122.       /* NOTREACHED */
  2123.       return 0;
  2124.  
  2125.     default:
  2126.       /* In the parent, after forking.
  2127.      Close the descriptors that we made for this child.  */
  2128.       if (input_desc != STDIN_FILE_NO)
  2129.     close (input_desc);
  2130.       if (output_desc != STDOUT_FILE_NO)
  2131.     close (output_desc);
  2132.  
  2133.       /* Return child's process number.  */
  2134.       return pid;
  2135.     }
  2136. }
  2137.  
  2138. #endif /* not __MSDOS__ and not OS2 and not _WIN32 */
  2139.  
  2140. #if defined(OS2) || defined(_WIN32)
  2141.  
  2142. #ifdef _WIN32
  2143.  
  2144. /* This is a kludge to get around the Microsoft C spawn functions' propensity
  2145.    to remove the outermost set of double quotes from all arguments.  */
  2146.  
  2147. const char * const
  2148. fix_argv (argvec)
  2149.      char **argvec;
  2150. {
  2151.   int i;
  2152.  
  2153.   for (i = 1; argvec[i] != 0; i++)
  2154.     {
  2155.       int len, j;
  2156.       char *temp, *newtemp;
  2157.  
  2158.       temp = argvec[i];
  2159.       len = strlen (temp);
  2160.       for (j = 0; j < len; j++)
  2161.         {
  2162.           if (temp[j] == '"')
  2163.             {
  2164.               newtemp = xmalloc (len + 2);
  2165.               strncpy (newtemp, temp, j);
  2166.               newtemp [j] = '\\';
  2167.               strncpy (&newtemp [j+1], &temp [j], len-j);
  2168.               newtemp [len+1] = 0;
  2169.               temp = newtemp;
  2170.               len++;
  2171.               j++;
  2172.             }
  2173.         }
  2174.  
  2175.         argvec[i] = temp;
  2176.       }
  2177.  
  2178.   return (const char* const*) argvec;
  2179. }
  2180.  
  2181. #define FIX_ARGV(a) fix_argv(a)
  2182.  
  2183. #else
  2184.  
  2185. #define FIX_ARGV(a) a
  2186.  
  2187. #endif
  2188.  
  2189. static int
  2190. pexecute (search_flag, program, argv, not_last)
  2191.      int search_flag;
  2192.      char *program;
  2193.      char *argv[];
  2194.      int not_last;
  2195. {
  2196.   return (search_flag ? spawnv : spawnvp) (1, program, FIX_ARGV (argv));
  2197. }
  2198. #endif /* OS2 or _WIN32 */
  2199. #endif /* !defined (PEXECUTE) */
  2200.  
  2201.  
  2202. /* Execute the command specified by the arguments on the current line of spec.
  2203.    When using pipes, this includes several piped-together commands
  2204.    with `|' between them.
  2205.  
  2206.    Return 0 if successful, -1 if failed.  */
  2207.  
  2208. static int
  2209. execute ()
  2210. {
  2211.   int i;
  2212.   int n_commands;        /* # of command.  */
  2213.   char *string;
  2214.   struct command
  2215.     {
  2216.       char *prog;        /* program name.  */
  2217.       char **argv;        /* vector of args.  */
  2218.       int pid;            /* pid of process for this command.  */
  2219.     };
  2220.  
  2221.   struct command *commands;    /* each command buffer with above info.  */
  2222.  
  2223.   /* Count # of piped commands.  */
  2224.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  2225.     if (strcmp (argbuf[i], "|") == 0)
  2226.       n_commands++;
  2227.  
  2228.   /* Get storage for each command.  */
  2229.   commands
  2230.     = (struct command *) alloca (n_commands * sizeof (struct command));
  2231.  
  2232.   /* Split argbuf into its separate piped processes,
  2233.      and record info about each one.
  2234.      Also search for the programs that are to be run.  */
  2235.  
  2236.   commands[0].prog = argbuf[0]; /* first command.  */
  2237.   commands[0].argv = &argbuf[0];
  2238.   string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
  2239.   if (string)
  2240.     commands[0].argv[0] = string;
  2241.  
  2242.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  2243.     if (strcmp (argbuf[i], "|") == 0)
  2244.       {                /* each command.  */
  2245. #ifdef __MSDOS__
  2246.         fatal ("-pipe not supported under MS-DOS");
  2247. #endif
  2248.     argbuf[i] = 0;    /* termination of command args.  */
  2249.     commands[n_commands].prog = argbuf[i + 1];
  2250.     commands[n_commands].argv = &argbuf[i + 1];
  2251.     string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
  2252.     if (string)
  2253.       commands[n_commands].argv[0] = string;
  2254.     n_commands++;
  2255.       }
  2256.  
  2257.   argbuf[argbuf_index] = 0;
  2258.  
  2259.   /* If -v, print what we are about to do, and maybe query.  */
  2260.  
  2261.   if (verbose_flag)
  2262.     {
  2263.       /* Print each piped command as a separate line.  */
  2264.       for (i = 0; i < n_commands ; i++)
  2265.     {
  2266.       char **j;
  2267.  
  2268.       for (j = commands[i].argv; *j; j++)
  2269.         fprintf (stderr, " %s", *j);
  2270.  
  2271.       /* Print a pipe symbol after all but the last command.  */
  2272.       if (i + 1 != n_commands)
  2273.         fprintf (stderr, " |");
  2274.       fprintf (stderr, "\n");
  2275.     }
  2276.       fflush (stderr);
  2277. #ifdef DEBUG
  2278.       fprintf (stderr, "\nGo ahead? (y or n) ");
  2279.       fflush (stderr);
  2280.       i = getchar ();
  2281.       if (i != '\n')
  2282.     while (getchar () != '\n') ;
  2283.       if (i != 'y' && i != 'Y')
  2284.     return 0;
  2285. #endif /* DEBUG */
  2286.     }
  2287.  
  2288.   /* Run each piped subprocess.  */
  2289.  
  2290.   last_pipe_input = STDIN_FILE_NO;
  2291.   for (i = 0; i < n_commands; i++)
  2292.     {
  2293.       char *string = commands[i].argv[0];
  2294.  
  2295. #ifdef PEXECUTE
  2296.       commands[i].pid = PEXECUTE (string != commands[i].prog,
  2297.                   string, commands[i].argv,
  2298.                   i + 1 < n_commands);
  2299. #else
  2300.       commands[i].pid = pexecute (string != commands[i].prog,
  2301.                   string, commands[i].argv,
  2302.                   i + 1 < n_commands);
  2303. #endif
  2304.  
  2305.       if (string != commands[i].prog)
  2306.     free (string);
  2307.     }
  2308.  
  2309.   execution_count++;
  2310.  
  2311.   /* Wait for all the subprocesses to finish.
  2312.      We don't care what order they finish in;
  2313.      we know that N_COMMANDS waits will get them all.
  2314.      Ignore subprocesses that we don't know about,
  2315.      since they can be spawned by the process that exec'ed us.  */
  2316.  
  2317.   {
  2318.     int ret_code = 0;
  2319.  
  2320.     for (i = 0; i < n_commands; )
  2321.       {
  2322.     int j;
  2323.     int status;
  2324.     int pid;
  2325.  
  2326. #ifdef PEXECUTE_RESULT
  2327.     pid = PEXECUTE_RESULT (status, commands[i]);
  2328. #else /* PEXECUTE_RESULT */
  2329. #ifdef __MSDOS__
  2330.         status = pid = commands[i].pid;
  2331. #else
  2332. #ifdef _WIN32
  2333.     pid = cwait (&status, commands[i].pid, WAIT_CHILD);
  2334. #else
  2335.     pid = wait (&status);
  2336. #endif
  2337. #endif
  2338. #endif /* PEXECUTE_RESULT */
  2339.     if (pid < 0)
  2340.       abort ();
  2341.  
  2342.     for (j = 0; j < n_commands; j++)
  2343.       if (commands[j].pid == pid)
  2344.         {
  2345.           i++;
  2346.           if (status != 0)
  2347.         {
  2348.           if (WIFSIGNALED (status))
  2349.             {
  2350.               fatal ("Internal compiler error: program %s got fatal signal %d",
  2351.                  commands[j].prog, WTERMSIG (status));
  2352.               signal_count++;
  2353.               ret_code = -1;
  2354.             }
  2355.           else if (WIFEXITED (status)
  2356.                && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
  2357.             ret_code = -1;
  2358.         }
  2359.           break;
  2360.         }
  2361.       }
  2362.     return ret_code;
  2363.   }
  2364. }
  2365.  
  2366. /* Find all the switches given to us
  2367.    and make a vector describing them.
  2368.    The elements of the vector are strings, one per switch given.
  2369.    If a switch uses following arguments, then the `part1' field
  2370.    is the switch itself and the `args' field
  2371.    is a null-terminated vector containing the following arguments.
  2372.    The `live_cond' field is 1 if the switch is true in a conditional spec,
  2373.    -1 if false (overridden by a later switch), and is initialized to zero.
  2374.    The `valid' field is nonzero if any spec has looked at this switch;
  2375.    if it remains zero at the end of the run, it must be meaningless.  */
  2376.  
  2377. struct switchstr
  2378. {
  2379.   char *part1;
  2380.   char **args;
  2381.   int live_cond;
  2382.   int valid;
  2383. };
  2384.  
  2385. static struct switchstr *switches;
  2386.  
  2387. static int n_switches;
  2388.  
  2389. struct infile
  2390. {
  2391.   char *name;
  2392.   char *language;
  2393. };
  2394.  
  2395. /* Also a vector of input files specified.  */
  2396.  
  2397. static struct infile *infiles;
  2398.  
  2399. static int n_infiles;
  2400.  
  2401. /* And a vector of corresponding output files is made up later.  */
  2402.  
  2403. static char **outfiles;
  2404.  
  2405. /* Create the vector `switches' and its contents.
  2406.    Store its length in `n_switches'.  */
  2407.  
  2408. static void
  2409. process_command (argc, argv)
  2410.      int argc;
  2411.      char **argv;
  2412. {
  2413.   register int i;
  2414.   char *temp;
  2415.   char *spec_lang = 0;
  2416.   int last_language_n_infiles;
  2417.  
  2418.   gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  2419.  
  2420.   n_switches = 0;
  2421.   n_infiles = 0;
  2422.  
  2423.   /* Figure compiler version from version string.  */
  2424.  
  2425.   compiler_version = save_string (version_string, strlen (version_string));
  2426.   for (temp = compiler_version; *temp; ++temp)
  2427.     {
  2428.       if (*temp == ' ')
  2429.     {
  2430.       *temp = '\0';
  2431.       break;
  2432.     }
  2433.     }
  2434.  
  2435.   /* Set up the default search paths.  */
  2436.  
  2437.   if (gcc_exec_prefix)
  2438.     {
  2439.       add_prefix (&exec_prefixes, gcc_exec_prefix, 0, 0, NULL_PTR);
  2440.       add_prefix (&startfile_prefixes, gcc_exec_prefix, 0, 0, NULL_PTR);
  2441.     }
  2442.  
  2443.   /* COMPILER_PATH and LIBRARY_PATH have values
  2444.      that are lists of directory names with colons.  */
  2445.  
  2446.   temp = getenv ("COMPILER_PATH");
  2447.   if (temp)
  2448.     {
  2449.       char *startp, *endp;
  2450.       char *nstore = (char *) alloca (strlen (temp) + 3);
  2451.  
  2452.       startp = endp = temp;
  2453.       while (1)
  2454.     {
  2455.       if (*endp == PATH_SEPARATOR || *endp == 0)
  2456.         {
  2457.           strncpy (nstore, startp, endp-startp);
  2458. #ifndef __amigados__
  2459.           if (endp == startp)
  2460.         strcpy (nstore, concat (".", dir_separator_str));
  2461.           else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
  2462.         {
  2463.           nstore[endp-startp] = DIR_SEPARATOR;
  2464.           nstore[endp-startp+1] = 0;
  2465.         }
  2466.           else
  2467.         nstore[endp-startp] = 0;
  2468. #else
  2469.           if (endp[-1] != '/' && endp[-1] != ':')
  2470.         {
  2471.           nstore[endp-startp] = '/';
  2472.           nstore[endp-startp+1] = 0;
  2473.         }
  2474.           else
  2475.         nstore[endp-startp] = 0;
  2476. #endif
  2477.           add_prefix (&exec_prefixes, nstore, 0, 0, NULL_PTR);
  2478.           if (*endp == 0)
  2479.         break;
  2480.           endp = startp = endp + 1;
  2481.         }
  2482.       else
  2483.         endp++;
  2484.     }
  2485.     }
  2486.  
  2487.   temp = getenv ("LIBRARY_PATH");
  2488.   if (temp && ! cross_compile)
  2489.     {
  2490.       char *startp, *endp;
  2491.       char *nstore = (char *) alloca (strlen (temp) + 3);
  2492.  
  2493.       startp = endp = temp;
  2494.       while (1)
  2495.     {
  2496.       if (*endp == PATH_SEPARATOR || *endp == 0)
  2497.         {
  2498.           strncpy (nstore, startp, endp-startp);
  2499. #ifndef __amigados__
  2500.           if (endp == startp)
  2501.         strcpy (nstore, concat (".", dir_separator_str));
  2502.           else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
  2503.         {
  2504.           nstore[endp-startp] = DIR_SEPARATOR;
  2505.           nstore[endp-startp+1] = 0;
  2506.         }
  2507.           else
  2508.         nstore[endp-startp] = 0;
  2509. #else
  2510.           if (endp[-1] != '/' && endp[-1] != ':')
  2511.         {
  2512.           nstore[endp-startp] = '/';
  2513.           nstore[endp-startp+1] = 0;
  2514.         }
  2515.           else
  2516.         nstore[endp-startp] = 0;
  2517. #endif
  2518.           add_prefix (&startfile_prefixes, nstore, 0, 0, NULL_PTR);
  2519.           if (*endp == 0)
  2520.         break;
  2521.           endp = startp = endp + 1;
  2522.         }
  2523.       else
  2524.         endp++;
  2525.     }
  2526.     }
  2527.  
  2528.   /* Use LPATH like LIBRARY_PATH (for the CMU build program).  */
  2529.   temp = getenv ("LPATH");
  2530.   if (temp && ! cross_compile)
  2531.     {
  2532.       char *startp, *endp;
  2533.       char *nstore = (char *) alloca (strlen (temp) + 3);
  2534.  
  2535.       startp = endp = temp;
  2536.       while (1)
  2537.     {
  2538.       if (*endp == PATH_SEPARATOR || *endp == 0)
  2539.         {
  2540.           strncpy (nstore, startp, endp-startp);
  2541. #ifndef __amigados__
  2542.           if (endp == startp)
  2543.         strcpy (nstore, concat (".", dir_separator_str));
  2544.           else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
  2545.         {
  2546.           nstore[endp-startp] = DIR_SEPARATOR;
  2547.           nstore[endp-startp+1] = 0;
  2548.         }
  2549.           else
  2550.         nstore[endp-startp] = 0;
  2551. #else
  2552.           if (endp[-1] != '/' && endp[-1] != ':')
  2553.         {
  2554.           nstore[endp-startp] = '/';
  2555.           nstore[endp-startp+1] = 0;
  2556.         }
  2557.           else
  2558.         nstore[endp-startp] = 0;
  2559. #endif
  2560.           add_prefix (&startfile_prefixes, nstore, 0, 0, NULL_PTR);
  2561.           if (*endp == 0)
  2562.         break;
  2563.           endp = startp = endp + 1;
  2564.         }
  2565.       else
  2566.         endp++;
  2567.     }
  2568.     }
  2569.  
  2570.   /* Convert new-style -- options to old-style.  */
  2571.   translate_options (&argc, &argv);
  2572.  
  2573.   /* Scan argv twice.  Here, the first time, just count how many switches
  2574.      there will be in their vector, and how many input files in theirs.
  2575.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  2576.  
  2577.   for (i = 1; i < argc; i++)
  2578.     {
  2579. #ifdef __amigados__
  2580.       /* Phil.B 03-Oct-94 added -priority keyword */
  2581.       if (! strcmp (argv[i], "-priority"))
  2582.         {
  2583.       if (i + 1 == argc)
  2584.         fatal ("argument to `-priority' is missing");
  2585.           amiga_priority = atoi(argv[++i]);
  2586.         }
  2587.       else
  2588. #endif /* __amigados__ */
  2589.       if (! strcmp (argv[i], "-dumpspecs"))
  2590.     {
  2591.       printf ("*asm:\n%s\n\n", asm_spec);
  2592.       printf ("*asm_final:\n%s\n\n", asm_final_spec);
  2593.       printf ("*cpp:\n%s\n\n", cpp_spec);
  2594.       printf ("*cc1:\n%s\n\n", cc1_spec);
  2595.       printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
  2596.       printf ("*endfile:\n%s\n\n", endfile_spec);
  2597.       printf ("*link:\n%s\n\n", link_spec);
  2598.       printf ("*lib:\n%s\n\n", lib_spec);
  2599.       printf ("*libgcc:\n%s\n\n", libgcc_spec);
  2600.       printf ("*startfile:\n%s\n\n", startfile_spec);
  2601.       printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
  2602.       printf ("*signed_char:\n%s\n\n", signed_char_spec);
  2603.       printf ("*predefines:\n%s\n\n", cpp_predefines);
  2604.       printf ("*cross_compile:\n%d\n\n", cross_compile);
  2605.       printf ("*multilib:\n%s\n\n", multilib_select);
  2606.  
  2607.       exit (0);
  2608.     }
  2609.       else if (! strcmp (argv[i], "-dumpversion"))
  2610.     {
  2611.       printf ("%s\n", version_string);
  2612.       exit (0);
  2613.     }
  2614.       else if (! strcmp (argv[i], "-dumpmachine"))
  2615.     {
  2616.       printf ("%s\n", spec_machine);
  2617.       exit  (0);
  2618.     }
  2619.       else if (! strcmp (argv[i], "-print-search-dirs"))
  2620.     print_search_dirs = 1;
  2621.       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
  2622.     print_file_name = "libgcc.a";
  2623.       else if (! strncmp (argv[i], "-print-file-name=", 17))
  2624.     print_file_name = argv[i] + 17;
  2625.       else if (! strncmp (argv[i], "-print-prog-name=", 17))
  2626.     print_prog_name = argv[i] + 17;
  2627.       else if (! strcmp (argv[i], "-print-multi-lib"))
  2628.     print_multi_lib = 1;
  2629.       else if (! strcmp (argv[i], "-print-multi-directory"))
  2630.     print_multi_directory = 1;
  2631.       else if (! strncmp (argv[i], "-Wa,", 4))
  2632.     {
  2633.       int prev, j;
  2634.       /* Pass the rest of this option to the assembler.  */
  2635.  
  2636.       n_assembler_options++;
  2637.       if (!assembler_options)
  2638.         assembler_options
  2639.           = (char **) xmalloc (n_assembler_options * sizeof (char **));
  2640.       else
  2641.         assembler_options
  2642.           = (char **) xrealloc (assembler_options,
  2643.                     n_assembler_options * sizeof (char **));
  2644.  
  2645.       /* Split the argument at commas.  */
  2646.       prev = 4;
  2647.       for (j = 4; argv[i][j]; j++)
  2648.         if (argv[i][j] == ',')
  2649.           {
  2650.         assembler_options[n_assembler_options - 1]
  2651.           = save_string (argv[i] + prev, j - prev);
  2652.         n_assembler_options++;
  2653.         assembler_options
  2654.           = (char **) xrealloc (assembler_options,
  2655.                     n_assembler_options * sizeof (char **));
  2656.         prev = j + 1;
  2657.           }
  2658.       /* Record the part after the last comma.  */
  2659.       assembler_options[n_assembler_options - 1] = argv[i] + prev;
  2660.     }
  2661.       else if (! strncmp (argv[i], "-Wp,", 4))
  2662.     {
  2663.       int prev, j;
  2664.       /* Pass the rest of this option to the preprocessor.  */
  2665.  
  2666.       n_preprocessor_options++;
  2667.       if (!preprocessor_options)
  2668.         preprocessor_options
  2669.           = (char **) xmalloc (n_preprocessor_options * sizeof (char **));
  2670.       else
  2671.         preprocessor_options
  2672.           = (char **) xrealloc (preprocessor_options,
  2673.                     n_preprocessor_options * sizeof (char **));
  2674.  
  2675.       /* Split the argument at commas.  */
  2676.       prev = 4;
  2677.       for (j = 4; argv[i][j]; j++)
  2678.         if (argv[i][j] == ',')
  2679.           {
  2680.         preprocessor_options[n_preprocessor_options - 1]
  2681.           = save_string (argv[i] + prev, j - prev);
  2682.         n_preprocessor_options++;
  2683.         preprocessor_options
  2684.           = (char **) xrealloc (preprocessor_options,
  2685.                     n_preprocessor_options * sizeof (char **));
  2686.         prev = j + 1;
  2687.           }
  2688.       /* Record the part after the last comma.  */
  2689.       preprocessor_options[n_preprocessor_options - 1] = argv[i] + prev;
  2690.     }
  2691.       else if (argv[i][0] == '+' && argv[i][1] == 'e')
  2692.     /* The +e options to the C++ front-end.  */
  2693.     n_switches++;
  2694.       else if (strncmp (argv[i], "-Wl,", 4) == 0)
  2695.     {
  2696.       int j;
  2697.       /* Split the argument at commas.  */
  2698.       for (j = 3; argv[i][j]; j++)
  2699.         n_infiles += (argv[i][j] == ',');
  2700.     }
  2701.       else if (strcmp (argv[i], "-Xlinker") == 0)
  2702.     {
  2703.       if (i + 1 == argc)
  2704.         fatal ("argument to `-Xlinker' is missing");
  2705.  
  2706.       n_infiles++;
  2707.       i++;
  2708.     }
  2709.       else if (strncmp (argv[i], "-l", 2) == 0)
  2710.     n_infiles++;
  2711.       else if (argv[i][0] == '-' && argv[i][1] != 0)
  2712.     {
  2713.       register char *p = &argv[i][1];
  2714.       register int c = *p;
  2715.  
  2716.       switch (c)
  2717.         {
  2718.         case 'b':
  2719.           if (p[1] == 0 && i + 1 == argc)
  2720.         fatal ("argument to `-b' is missing");
  2721.           if (p[1] == 0)
  2722.         spec_machine = argv[++i];
  2723.           else
  2724.         spec_machine = p + 1;
  2725.           break;
  2726.  
  2727.         case 'B':
  2728.           {
  2729.         int *temp = (int *) xmalloc (sizeof (int));
  2730.         char *value;
  2731.         if (p[1] == 0 && i + 1 == argc)
  2732.           fatal ("argument to `-B' is missing");
  2733.         if (p[1] == 0)
  2734.           value = argv[++i];
  2735.         else
  2736.           value = p + 1;
  2737.         add_prefix (&exec_prefixes, value, 1, 0, temp);
  2738.         add_prefix (&startfile_prefixes, value, 1, 0, temp);
  2739.         add_prefix (&include_prefixes, concat (value, "include"),
  2740.                 1, 0, 0);
  2741.  
  2742.         /* As a kludge, if the arg is "[foo/]stageN/", just add
  2743.            "[foo/]include" to the include prefix.  */
  2744.         {
  2745.           int len = strlen (value);
  2746.           if ((len == 7
  2747.                || (len > 7
  2748.                && (value[len - 8] == '/'
  2749.                    || value[len - 8] == DIR_SEPARATOR)))
  2750.               && strncmp (value + len - 7, "stage", 5) == 0
  2751.               && isdigit (value[len - 2])
  2752.               && (value[len - 1] == '/'
  2753.               || value[len - 1] == DIR_SEPARATOR))
  2754.             {
  2755.               if (len == 7)
  2756.             add_prefix (&include_prefixes, "include", 1, 0, 0);
  2757.               else
  2758.             {
  2759.               char *string = xmalloc (len + 1);
  2760.               strncpy (string, value, len-7);
  2761.               strcat (string, "include");
  2762.               add_prefix (&include_prefixes, string, 1, 0, 0);
  2763.             }
  2764.             }
  2765.         }
  2766.           }
  2767.           break;
  2768.  
  2769.         case 'v':    /* Print our subcommands and print versions.  */
  2770.           n_switches++;
  2771.           /* If they do anything other than exactly `-v', don't set
  2772.          verbose_flag; rather, continue on to give the error.  */
  2773.           if (p[1] != 0)
  2774.         break;
  2775.           verbose_flag++;
  2776.           break;
  2777.  
  2778.         case 'V':
  2779.           if (p[1] == 0 && i + 1 == argc)
  2780.         fatal ("argument to `-V' is missing");
  2781.           if (p[1] == 0)
  2782.         spec_version = argv[++i];
  2783.           else
  2784.         spec_version = p + 1;
  2785.           compiler_version = spec_version;
  2786.           break;
  2787.  
  2788.         case 's':
  2789.           if (!strcmp (p, "save-temps"))
  2790.         {
  2791.           save_temps_flag = 1;
  2792.           n_switches++;
  2793.           break;
  2794.         }
  2795.         default:
  2796.           n_switches++;
  2797.  
  2798.           if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
  2799.         i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
  2800.           else if (WORD_SWITCH_TAKES_ARG (p))
  2801.         i += WORD_SWITCH_TAKES_ARG (p);
  2802.         }
  2803.     }
  2804.       else
  2805.     n_infiles++;
  2806.     }
  2807.  
  2808.   /* Set up the search paths before we go looking for config files.  */
  2809.  
  2810.   /* These come before the md prefixes so that we will find gcc's subcommands
  2811.      (such as cpp) rather than those of the host system.  */
  2812.   /* Use 2 as fourth arg meaning try just the machine as a suffix,
  2813.      as well as trying the machine and the version.  */
  2814. #ifndef OS2
  2815.   add_prefix (&exec_prefixes, standard_exec_prefix, 0, 2, NULL_PTR);
  2816.   add_prefix (&exec_prefixes, standard_exec_prefix_1, 0, 2, NULL_PTR);
  2817. #endif
  2818.  
  2819.   add_prefix (&startfile_prefixes, standard_exec_prefix, 0, 1, NULL_PTR);
  2820.   add_prefix (&startfile_prefixes, standard_exec_prefix_1, 0, 1, NULL_PTR);
  2821.  
  2822.   tooldir_prefix = concat3 (tooldir_base_prefix, spec_machine, 
  2823.                             dir_separator_str);
  2824.  
  2825.   /* If tooldir is relative, base it on exec_prefixes.  A relative
  2826.      tooldir lets us move the installed tree as a unit.
  2827.  
  2828.      If GCC_EXEC_PREFIX is defined, then we want to add two relative
  2829.      directories, so that we can search both the user specified directory
  2830.      and the standard place.  */
  2831.  
  2832.   if (*tooldir_prefix != '/' && *tooldir_prefix != DIR_SEPARATOR)
  2833.     {
  2834.       if (gcc_exec_prefix)
  2835.     {
  2836.       char *gcc_exec_tooldir_prefix
  2837.         = concat6 (gcc_exec_prefix, spec_machine, dir_separator_str,
  2838.               spec_version, dir_separator_str, tooldir_prefix);
  2839.  
  2840.       add_prefix (&exec_prefixes,
  2841.               concat3 (gcc_exec_tooldir_prefix, "bin", 
  2842.                                dir_separator_str),
  2843.               0, 0, NULL_PTR);
  2844.       add_prefix (&startfile_prefixes,
  2845.               concat3 (gcc_exec_tooldir_prefix, "lib", 
  2846.                                dir_separator_str),
  2847.               0, 0, NULL_PTR);
  2848.     }
  2849.  
  2850.       tooldir_prefix = concat6 (standard_exec_prefix, spec_machine,
  2851.                     dir_separator_str, spec_version, 
  2852.                                 dir_separator_str, tooldir_prefix);
  2853.     }
  2854.  
  2855.   add_prefix (&exec_prefixes, 
  2856.               concat3 (tooldir_prefix, "bin", dir_separator_str),
  2857.           0, 0, NULL_PTR);
  2858.   add_prefix (&startfile_prefixes,
  2859.           concat3 (tooldir_prefix, "lib", dir_separator_str),
  2860.           0, 0, NULL_PTR);
  2861.  
  2862.   /* More prefixes are enabled in main, after we read the specs file
  2863.      and determine whether this is cross-compilation or not.  */
  2864.  
  2865.  
  2866.   /* Then create the space for the vectors and scan again.  */
  2867.  
  2868.   switches = ((struct switchstr *)
  2869.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  2870.   infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
  2871.   n_switches = 0;
  2872.   n_infiles = 0;
  2873.   last_language_n_infiles = -1;
  2874.  
  2875.   /* This, time, copy the text of each switch and store a pointer
  2876.      to the copy in the vector of switches.
  2877.      Store all the infiles in their vector.  */
  2878.  
  2879.   for (i = 1; i < argc; i++)
  2880.     {
  2881.       /* Just skip the switches that were handled by the preceding loop.  */
  2882.       if (! strncmp (argv[i], "-Wa,", 4))
  2883.     ;
  2884.       else if (! strncmp (argv[i], "-Wp,", 4))
  2885.     ;
  2886.       else if (! strcmp (argv[i], "-print-search-dirs"))
  2887.     ;
  2888.       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
  2889.     ;
  2890.       else if (! strncmp (argv[i], "-print-file-name=", 17))
  2891.     ;
  2892.       else if (! strncmp (argv[i], "-print-prog-name=", 17))
  2893.     ;
  2894.       else if (! strcmp (argv[i], "-print-multi-lib"))
  2895.     ;
  2896.       else if (! strcmp (argv[i], "-print-multi-directory"))
  2897.     ;
  2898.       else if (argv[i][0] == '+' && argv[i][1] == 'e')
  2899.     {
  2900.       /* Compensate for the +e options to the C++ front-end;
  2901.          they're there simply for cfront call-compatibility.  We do
  2902.          some magic in default_compilers to pass them down properly.
  2903.          Note we deliberately start at the `+' here, to avoid passing
  2904.          -e0 or -e1 down into the linker.  */
  2905.       switches[n_switches].part1 = &argv[i][0];
  2906.       switches[n_switches].args = 0;
  2907.       switches[n_switches].live_cond = 0;
  2908.       switches[n_switches].valid = 0;
  2909.       n_switches++;
  2910.     }
  2911.       else if (strncmp (argv[i], "-Wl,", 4) == 0)
  2912.     {
  2913.       int prev, j;
  2914.       /* Split the argument at commas.  */
  2915.       prev = 4;
  2916.       for (j = 4; argv[i][j]; j++)
  2917.         if (argv[i][j] == ',')
  2918.           {
  2919.         infiles[n_infiles].language = 0;
  2920.         infiles[n_infiles++].name
  2921.           = save_string (argv[i] + prev, j - prev);
  2922.         prev = j + 1;
  2923.           }
  2924.       /* Record the part after the last comma.  */
  2925.       infiles[n_infiles].language = 0;
  2926.       infiles[n_infiles++].name = argv[i] + prev;
  2927.     }
  2928.       else if (strcmp (argv[i], "-Xlinker") == 0)
  2929.     {
  2930.       infiles[n_infiles].language = 0;
  2931.       infiles[n_infiles++].name = argv[++i];
  2932.     }
  2933.       else if (strncmp (argv[i], "-l", 2) == 0)
  2934.     {
  2935.       infiles[n_infiles].language = 0;
  2936.       infiles[n_infiles++].name = argv[i];
  2937.     }
  2938.       else if (argv[i][0] == '-' && argv[i][1] != 0)
  2939.     {
  2940.       register char *p = &argv[i][1];
  2941.       register int c = *p;
  2942.  
  2943.       if (c == 'B' || c == 'b' || c == 'V')
  2944.         {
  2945.           /* Skip a separate arg, if any.  */
  2946.           if (p[1] == 0)
  2947.         i++;
  2948.           continue;
  2949.         }
  2950.       if (c == 'x')
  2951.         {
  2952.           if (p[1] == 0 && i + 1 == argc)
  2953.         fatal ("argument to `-x' is missing");
  2954.           if (p[1] == 0)
  2955.         spec_lang = argv[++i];
  2956.           else
  2957.         spec_lang = p + 1;
  2958.           if (! strcmp (spec_lang, "none"))
  2959.         /* Suppress the warning if -xnone comes after the last input
  2960.            file, because alternate command interfaces like g++ might
  2961.            find it useful to place -xnone after each input file.  */
  2962.         spec_lang = 0;
  2963.           else
  2964.         last_language_n_infiles = n_infiles;
  2965.           continue;
  2966.         }
  2967.       switches[n_switches].part1 = p;
  2968.       /* Deal with option arguments in separate argv elements.  */
  2969.       if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
  2970.           || WORD_SWITCH_TAKES_ARG (p))
  2971.         {
  2972.           int j = 0;
  2973.           int n_args = WORD_SWITCH_TAKES_ARG (p);
  2974.  
  2975.           if (n_args == 0)
  2976.         {
  2977.           /* Count only the option arguments in separate argv elements.  */
  2978.           n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
  2979.         }
  2980.           if (i + n_args >= argc)
  2981.         fatal ("argument to `-%s' is missing", p);
  2982.           switches[n_switches].args
  2983.         = (char **) xmalloc ((n_args + 1) * sizeof (char *));
  2984.           while (j < n_args)
  2985.         switches[n_switches].args[j++] = argv[++i];
  2986.           /* Null-terminate the vector.  */
  2987.           switches[n_switches].args[j] = 0;
  2988.         }
  2989.       else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L'))
  2990.         {
  2991.           /* On some systems, ld cannot handle -o or -L without space.
  2992.          So split the -o or -L from its argument.  */
  2993.           switches[n_switches].part1 = (c == 'o' ? "o" : "L");
  2994.           switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
  2995.           switches[n_switches].args[0] = xmalloc (strlen (p));
  2996.           strcpy (switches[n_switches].args[0], &p[1]);
  2997.           switches[n_switches].args[1] = 0;
  2998.         }
  2999.       else
  3000.         switches[n_switches].args = 0;
  3001.  
  3002.       switches[n_switches].live_cond = 0;
  3003.       switches[n_switches].valid = 0;
  3004.       /* This is always valid, since gcc.c itself understands it.  */
  3005.       if (!strcmp (p, "save-temps"))
  3006.         switches[n_switches].valid = 1;
  3007.       n_switches++;
  3008.     }
  3009.       else
  3010.     {
  3011. #ifdef HAVE_OBJECT_SUFFIX
  3012.       /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj".  */
  3013.       if (strlen (argv[i]) > 2
  3014.           && argv[i][strlen (argv[i]) - 2] == '.'
  3015.           && argv[i][strlen (argv[i]) - 1] == 'o')
  3016.         {
  3017.           int j;
  3018.  
  3019.           for (j = 0; j < strlen (argv[i]) - 2; j++)
  3020.         obstack_1grow (&obstack, argv[i][j]);
  3021.  
  3022.           obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
  3023.           obstack_1grow (&obstack, 0);
  3024.           argv[i] = obstack_finish (&obstack);
  3025.         }
  3026. #endif
  3027.  
  3028.       if (strcmp (argv[i], "-") != 0 && access (argv[i], R_OK) < 0)
  3029.         {
  3030.           perror_with_name (argv[i]);
  3031.           error_count++;
  3032.         }
  3033.       else
  3034.         {
  3035.           infiles[n_infiles].language = spec_lang;
  3036.           infiles[n_infiles++].name = argv[i];
  3037.         }
  3038.     }
  3039.     }
  3040.  
  3041.   if (n_infiles == last_language_n_infiles && spec_lang != 0)
  3042.     error ("Warning: `-x %s' after last input file has no effect", spec_lang);
  3043.  
  3044.   switches[n_switches].part1 = 0;
  3045.   infiles[n_infiles].name = 0;
  3046. }
  3047.  
  3048. /* Process a spec string, accumulating and running commands.  */
  3049.  
  3050. /* These variables describe the input file name.
  3051.    input_file_number is the index on outfiles of this file,
  3052.    so that the output file name can be stored for later use by %o.
  3053.    input_basename is the start of the part of the input file
  3054.    sans all directory names, and basename_length is the number
  3055.    of characters starting there excluding the suffix .c or whatever.  */
  3056.  
  3057. static char *input_filename;
  3058. static int input_file_number;
  3059. static int input_filename_length;
  3060. static int basename_length;
  3061. static char *input_basename;
  3062. static char *input_suffix;
  3063.  
  3064. /* These are variables used within do_spec and do_spec_1.  */
  3065.  
  3066. /* Nonzero if an arg has been started and not yet terminated
  3067.    (with space, tab or newline).  */
  3068. static int arg_going;
  3069.  
  3070. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  3071.    is a temporary file name.  */
  3072. static int delete_this_arg;
  3073.  
  3074. /* Nonzero means %w has been seen; the next arg to be terminated
  3075.    is the output file name of this compilation.  */
  3076. static int this_is_output_file;
  3077.  
  3078. /* Nonzero means %s has been seen; the next arg to be terminated
  3079.    is the name of a library file and we should try the standard
  3080.    search dirs for it.  */
  3081. static int this_is_library_file;
  3082.  
  3083. /* Nonzero means that the input of this command is coming from a pipe.  */
  3084. static int input_from_pipe;
  3085.  
  3086. /* Process the spec SPEC and run the commands specified therein.
  3087.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  3088.  
  3089. static int
  3090. do_spec (spec)
  3091.      char *spec;
  3092. {
  3093.   int value;
  3094.  
  3095.   clear_args ();
  3096.   arg_going = 0;
  3097.   delete_this_arg = 0;
  3098.   this_is_output_file = 0;
  3099.   this_is_library_file = 0;
  3100.   input_from_pipe = 0;
  3101.  
  3102.   value = do_spec_1 (spec, 0, NULL_PTR);
  3103.  
  3104.   /* Force out any unfinished command.
  3105.      If -pipe, this forces out the last command if it ended in `|'.  */
  3106.   if (value == 0)
  3107.     {
  3108.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  3109.     argbuf_index--;
  3110.  
  3111.       if (argbuf_index > 0)
  3112.     value = execute ();
  3113.     }
  3114.  
  3115.   return value;
  3116. }
  3117.  
  3118. /* Process the sub-spec SPEC as a portion of a larger spec.
  3119.    This is like processing a whole spec except that we do
  3120.    not initialize at the beginning and we do not supply a
  3121.    newline by default at the end.
  3122.    INSWITCH nonzero means don't process %-sequences in SPEC;
  3123.    in this case, % is treated as an ordinary character.
  3124.    This is used while substituting switches.
  3125.    INSWITCH nonzero also causes SPC not to terminate an argument.
  3126.  
  3127.    Value is zero unless a line was finished
  3128.    and the command on that line reported an error.  */
  3129.  
  3130. static int
  3131. do_spec_1 (spec, inswitch, soft_matched_part)
  3132.      char *spec;
  3133.      int inswitch;
  3134.      char *soft_matched_part;
  3135. {
  3136.   register char *p = spec;
  3137.   register int c;
  3138.   int i;
  3139.   char *string;
  3140.   int value;
  3141.  
  3142.   while (c = *p++)
  3143.     /* If substituting a switch, treat all chars like letters.
  3144.        Otherwise, NL, SPC, TAB and % are special.  */
  3145.     switch (inswitch ? 'a' : c)
  3146.       {
  3147.       case '\n':
  3148.     /* End of line: finish any pending argument,
  3149.        then run the pending command if one has been started.  */
  3150.     if (arg_going)
  3151.       {
  3152.         obstack_1grow (&obstack, 0);
  3153.         string = obstack_finish (&obstack);
  3154.         if (this_is_library_file)
  3155.           string = find_file (string);
  3156.         store_arg (string, delete_this_arg, this_is_output_file);
  3157.         if (this_is_output_file)
  3158.           outfiles[input_file_number] = string;
  3159.       }
  3160.     arg_going = 0;
  3161.  
  3162.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  3163.       {
  3164.         for (i = 0; i < n_switches; i++)
  3165.           if (!strcmp (switches[i].part1, "pipe"))
  3166.         break;
  3167.  
  3168.         /* A `|' before the newline means use a pipe here,
  3169.            but only if -pipe was specified.
  3170.            Otherwise, execute now and don't pass the `|' as an arg.  */
  3171.         if (i < n_switches)
  3172.           {
  3173.         input_from_pipe = 1;
  3174.         switches[i].valid = 1;
  3175.         break;
  3176.           }
  3177.         else
  3178.           argbuf_index--;
  3179.       }
  3180.  
  3181.     if (argbuf_index > 0)
  3182.       {
  3183.         value = execute ();
  3184.         if (value)
  3185.           return value;
  3186.       }
  3187.     /* Reinitialize for a new command, and for a new argument.  */
  3188.     clear_args ();
  3189.     arg_going = 0;
  3190.     delete_this_arg = 0;
  3191.     this_is_output_file = 0;
  3192.     this_is_library_file = 0;
  3193.     input_from_pipe = 0;
  3194.     break;
  3195.  
  3196.       case '|':
  3197.     /* End any pending argument.  */
  3198.     if (arg_going)
  3199.       {
  3200.         obstack_1grow (&obstack, 0);
  3201.         string = obstack_finish (&obstack);
  3202.         if (this_is_library_file)
  3203.           string = find_file (string);
  3204.         store_arg (string, delete_this_arg, this_is_output_file);
  3205.         if (this_is_output_file)
  3206.           outfiles[input_file_number] = string;
  3207.       }
  3208.  
  3209.     /* Use pipe */
  3210.     obstack_1grow (&obstack, c);
  3211.     arg_going = 1;
  3212.     break;
  3213.  
  3214.       case '\t':
  3215.       case ' ':
  3216.     /* Space or tab ends an argument if one is pending.  */
  3217.     if (arg_going)
  3218.       {
  3219.         obstack_1grow (&obstack, 0);
  3220.         string = obstack_finish (&obstack);
  3221.         if (this_is_library_file)
  3222.           string = find_file (string);
  3223.         store_arg (string, delete_this_arg, this_is_output_file);
  3224.         if (this_is_output_file)
  3225.           outfiles[input_file_number] = string;
  3226.       }
  3227.     /* Reinitialize for a new argument.  */
  3228.     arg_going = 0;
  3229.     delete_this_arg = 0;
  3230.     this_is_output_file = 0;
  3231.     this_is_library_file = 0;
  3232.     break;
  3233.  
  3234.       case '%':
  3235.     switch (c = *p++)
  3236.       {
  3237.       case 0:
  3238.         fatal ("Invalid specification!  Bug in cc.");
  3239.  
  3240.       case 'b':
  3241.         obstack_grow (&obstack, input_basename, basename_length);
  3242.         arg_going = 1;
  3243.         break;
  3244.  
  3245.       case 'd':
  3246.         delete_this_arg = 2;
  3247.         break;
  3248.  
  3249.       /* Dump out the directories specified with LIBRARY_PATH,
  3250.          followed by the absolute directories
  3251.          that we search for startfiles.  */
  3252.       case 'D':
  3253.         {
  3254.           struct prefix_list *pl = startfile_prefixes.plist;
  3255.           int bufsize = 100;
  3256.           char *buffer = (char *) xmalloc (bufsize);
  3257.           int idx;
  3258.  
  3259.           for (; pl; pl = pl->next)
  3260.         {
  3261. #ifdef RELATIVE_PREFIX_NOT_LINKDIR
  3262.           /* Used on systems which record the specified -L dirs
  3263.              and use them to search for dynamic linking.  */
  3264.           /* Relative directories always come from -B,
  3265.              and it is better not to use them for searching
  3266.              at run time.  In particular, stage1 loses  */
  3267.           if (pl->prefix[0] != '/' && pl->prefix[0] != DIR_SEPARATOR)
  3268.             continue;
  3269. #endif
  3270.           /* Try subdirectory if there is one.  */
  3271.           if (multilib_dir != NULL)
  3272.             {
  3273.               if (machine_suffix)
  3274.             {
  3275.               if (strlen (pl->prefix) + strlen (machine_suffix)
  3276.                   >= bufsize)
  3277.                 bufsize = (strlen (pl->prefix)
  3278.                        + strlen (machine_suffix)) * 2 + 1;
  3279.               buffer = (char *) xrealloc (buffer, bufsize);
  3280.               strcpy (buffer, pl->prefix);
  3281.               strcat (buffer, machine_suffix);
  3282.               if (is_directory (buffer, multilib_dir, 1))
  3283.                 {
  3284.                   do_spec_1 ("-L", 0, NULL_PTR);
  3285. #ifdef SPACE_AFTER_L_OPTION
  3286.                   do_spec_1 (" ", 0, NULL_PTR);
  3287. #endif
  3288.                   do_spec_1 (buffer, 1, NULL_PTR);
  3289.                   do_spec_1 (multilib_dir, 1, NULL_PTR);
  3290.                   /* Make this a separate argument.  */
  3291.                   do_spec_1 (" ", 0, NULL_PTR);
  3292.                 }
  3293.             }
  3294.               if (!pl->require_machine_suffix)
  3295.             {
  3296.               if (is_directory (pl->prefix, multilib_dir, 1))
  3297.                 {
  3298.                   do_spec_1 ("-L", 0, NULL_PTR);
  3299. #ifdef SPACE_AFTER_L_OPTION
  3300.                   do_spec_1 (" ", 0, NULL_PTR);
  3301. #endif
  3302.                   do_spec_1 (pl->prefix, 1, NULL_PTR);
  3303.                   do_spec_1 (multilib_dir, 1, NULL_PTR);
  3304.                   /* Make this a separate argument.  */
  3305.                   do_spec_1 (" ", 0, NULL_PTR);
  3306.                 }
  3307.             }
  3308.             }
  3309.           if (machine_suffix)
  3310.             {
  3311.               if (is_directory (pl->prefix, machine_suffix, 1))
  3312.             {
  3313.               do_spec_1 ("-L", 0, NULL_PTR);
  3314. #ifdef SPACE_AFTER_L_OPTION
  3315.               do_spec_1 (" ", 0, NULL_PTR);
  3316. #endif
  3317.               do_spec_1 (pl->prefix, 1, NULL_PTR);
  3318.               /* Remove slash from machine_suffix.  */
  3319.               if (strlen (machine_suffix) >= bufsize)
  3320.                 bufsize = strlen (machine_suffix) * 2 + 1;
  3321.               buffer = (char *) xrealloc (buffer, bufsize);
  3322.               strcpy (buffer, machine_suffix);
  3323.               idx = strlen (buffer);
  3324.               if (buffer[idx - 1] == '/'
  3325.                   || buffer[idx - 1] == DIR_SEPARATOR)
  3326.                 buffer[idx - 1] = 0;
  3327.               do_spec_1 (buffer, 1, NULL_PTR);
  3328.               /* Make this a separate argument.  */
  3329.               do_spec_1 (" ", 0, NULL_PTR);
  3330.             }
  3331.             }
  3332.           if (!pl->require_machine_suffix)
  3333.             {
  3334.               if (is_directory (pl->prefix, "", 1))
  3335.             {
  3336.               do_spec_1 ("-L", 0, NULL_PTR);
  3337. #ifdef SPACE_AFTER_L_OPTION
  3338.               do_spec_1 (" ", 0, NULL_PTR);
  3339. #endif
  3340.               /* Remove slash from pl->prefix.  */
  3341.               if (strlen (pl->prefix) >= bufsize)
  3342.                 bufsize = strlen (pl->prefix) * 2 + 1;
  3343.               buffer = (char *) xrealloc (buffer, bufsize);
  3344.               strcpy (buffer, pl->prefix);
  3345.               idx = strlen (buffer);
  3346.               if (buffer[idx - 1] == '/'
  3347.                   || buffer[idx - 1] == DIR_SEPARATOR)
  3348.                 buffer[idx - 1] = 0;
  3349.               do_spec_1 (buffer, 1, NULL_PTR);
  3350.               /* Make this a separate argument.  */
  3351.               do_spec_1 (" ", 0, NULL_PTR);
  3352.             }
  3353.             }
  3354.         }
  3355.           free (buffer);
  3356.         }
  3357.         break;
  3358.  
  3359.       case 'e':
  3360.         /* {...:%efoo} means report an error with `foo' as error message
  3361.            and don't execute any more commands for this file.  */
  3362.         {
  3363.           char *q = p;
  3364.           char *buf;
  3365.           while (*p != 0 && *p != '\n') p++;
  3366.           buf = (char *) alloca (p - q + 1);
  3367.           strncpy (buf, q, p - q);
  3368.           buf[p - q] = 0;
  3369.           error ("%s", buf);
  3370.           return -1;
  3371.         }
  3372.         break;
  3373.  
  3374.       case 'g':
  3375.       case 'u':
  3376.       case 'U':
  3377.         if (save_temps_flag)
  3378.           {
  3379.         obstack_grow (&obstack, input_basename, basename_length);
  3380.         delete_this_arg = 0;
  3381.           }
  3382.         else
  3383.           {
  3384. #ifdef MKTEMP_EACH_FILE
  3385.         /* ??? This has a problem: the total number of
  3386.            values mktemp can return is limited.
  3387.            That matters for the names of object files.
  3388.            In 2.4, do something about that.  */
  3389.         struct temp_name *t;
  3390.         char *suffix = p;
  3391.         while (*p == '.' || isalpha (*p)
  3392.                || (p[0] == '%' && p[1] == 'O'))
  3393.           p++;
  3394.  
  3395.         /* See if we already have an association of %g/%u/%U and
  3396.            suffix.  */
  3397.         for (t = temp_names; t; t = t->next)
  3398.           if (t->length == p - suffix
  3399.               && strncmp (t->suffix, suffix, p - suffix) == 0
  3400.               && t->unique == (c != 'g'))
  3401.             break;
  3402.  
  3403.         /* Make a new association if needed.  %u requires one.  */
  3404.         if (t == 0 || c == 'u')
  3405.           {
  3406.             if (t == 0)
  3407.               {
  3408.             t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
  3409.             t->next = temp_names;
  3410.             temp_names = t;
  3411.               }
  3412.             t->length = p - suffix;
  3413.             t->suffix = save_string (suffix, p - suffix);
  3414.             t->unique = (c != 'g');
  3415.             choose_temp_base ();
  3416.             t->filename = temp_filename;
  3417.             t->filename_length = temp_filename_length;
  3418.           }
  3419.  
  3420.         obstack_grow (&obstack, t->filename, t->filename_length);
  3421.         delete_this_arg = 1;
  3422. #else
  3423.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  3424.         if (c == 'u' || c == 'U')
  3425.           {
  3426.             static int unique;
  3427.             char buff[9];
  3428.             if (c == 'u')
  3429.               unique++;
  3430.             sprintf (buff, "%d", unique);
  3431.             obstack_grow (&obstack, buff, strlen (buff));
  3432.           }
  3433. #endif
  3434.         delete_this_arg = 1;
  3435.           }
  3436.         arg_going = 1;
  3437.         break;
  3438.  
  3439.       case 'i':
  3440.         obstack_grow (&obstack, input_filename, input_filename_length);
  3441.         arg_going = 1;
  3442.         break;
  3443.  
  3444.       case 'I':
  3445.         {
  3446.           struct prefix_list *pl = include_prefixes.plist;
  3447.  
  3448.           if (gcc_exec_prefix)
  3449.         {
  3450.           do_spec_1 ("-iprefix", 1, NULL_PTR);
  3451.           /* Make this a separate argument.  */
  3452.           do_spec_1 (" ", 0, NULL_PTR);
  3453.           do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
  3454.           do_spec_1 (" ", 0, NULL_PTR);
  3455.         }
  3456.  
  3457.           for (; pl; pl = pl->next)
  3458.         {
  3459.           do_spec_1 ("-isystem", 1, NULL_PTR);
  3460.           /* Make this a separate argument.  */
  3461.           do_spec_1 (" ", 0, NULL_PTR);
  3462.           do_spec_1 (pl->prefix, 1, NULL_PTR);
  3463.           do_spec_1 (" ", 0, NULL_PTR);
  3464.         }
  3465.         }
  3466.         break;
  3467.  
  3468.       case 'o':
  3469.         for (i = 0; i < n_infiles; i++)
  3470.           store_arg (outfiles[i], 0, 0);
  3471.         break;
  3472.  
  3473.       case 'O':
  3474.         obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
  3475.         arg_going = 1;
  3476.         break;
  3477.  
  3478.       case 's':
  3479.         this_is_library_file = 1;
  3480.         break;
  3481.  
  3482.       case 'w':
  3483.         this_is_output_file = 1;
  3484.         break;
  3485.  
  3486.       case 'W':
  3487.         {
  3488.           int index = argbuf_index;
  3489.           /* Handle the {...} following the %W.  */
  3490.           if (*p != '{')
  3491.         abort ();
  3492.           p = handle_braces (p + 1);
  3493.           if (p == 0)
  3494.         return -1;
  3495.           /* If any args were output, mark the last one for deletion
  3496.          on failure.  */
  3497.           if (argbuf_index != index)
  3498.         record_temp_file (argbuf[argbuf_index - 1], 0, 1);
  3499.           break;
  3500.         }
  3501.  
  3502.       /* %x{OPTION} records OPTION for %X to output.  */
  3503.       case 'x':
  3504.         {
  3505.           char *p1 = p;
  3506.           char *string;
  3507.  
  3508.           /* Skip past the option value and make a copy.  */
  3509.           if (*p != '{')
  3510.         abort ();
  3511.           while (*p++ != '}')
  3512.         ;
  3513.           string = save_string (p1 + 1, p - p1 - 2);
  3514.  
  3515.           /* See if we already recorded this option.  */
  3516.           for (i = 0; i < n_linker_options; i++)
  3517.         if (! strcmp (string, linker_options[i]))
  3518.           {
  3519.             free (string);
  3520.             return 0;
  3521.           }
  3522.  
  3523.           /* This option is new; add it.  */
  3524.           n_linker_options++;
  3525.           if (!linker_options)
  3526.         linker_options
  3527.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  3528.           else
  3529.         linker_options
  3530.           = (char **) xrealloc (linker_options,
  3531.                     n_linker_options * sizeof (char **));
  3532.  
  3533.           linker_options[n_linker_options - 1] = string;
  3534.         }
  3535.         break;
  3536.  
  3537.       /* Dump out the options accumulated previously using %x.  */
  3538.       case 'X':
  3539.         for (i = 0; i < n_linker_options; i++)
  3540.           {
  3541.         do_spec_1 (linker_options[i], 1, NULL_PTR);
  3542.         /* Make each accumulated option a separate argument.  */
  3543.         do_spec_1 (" ", 0, NULL_PTR);
  3544.           }
  3545.         break;
  3546.  
  3547.       /* Dump out the options accumulated previously using -Wa,.  */
  3548.       case 'Y':
  3549.         for (i = 0; i < n_assembler_options; i++)
  3550.           {
  3551.         do_spec_1 (assembler_options[i], 1, NULL_PTR);
  3552.         /* Make each accumulated option a separate argument.  */
  3553.         do_spec_1 (" ", 0, NULL_PTR);
  3554.           }
  3555.         break;
  3556.  
  3557.       /* Dump out the options accumulated previously using -Wp,.  */
  3558.       case 'Z':
  3559.         for (i = 0; i < n_preprocessor_options; i++)
  3560.           {
  3561.         do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
  3562.         /* Make each accumulated option a separate argument.  */
  3563.         do_spec_1 (" ", 0, NULL_PTR);
  3564.           }
  3565.         break;
  3566.  
  3567.         /* Here are digits and numbers that just process
  3568.            a certain constant string as a spec.  */
  3569.  
  3570.       case '1':
  3571.         value = do_spec_1 (cc1_spec, 0, NULL_PTR);
  3572.         if (value != 0)
  3573.           return value;
  3574.         break;
  3575.  
  3576.       case '2':
  3577.         value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
  3578.         if (value != 0)
  3579.           return value;
  3580.         break;
  3581.  
  3582.       case 'a':
  3583.         value = do_spec_1 (asm_spec, 0, NULL_PTR);
  3584.         if (value != 0)
  3585.           return value;
  3586.         break;
  3587.  
  3588.       case 'A':
  3589.         value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
  3590.         if (value != 0)
  3591.           return value;
  3592.         break;
  3593.  
  3594.       case 'c':
  3595.         value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
  3596.         if (value != 0)
  3597.           return value;
  3598.         break;
  3599.  
  3600.       case 'C':
  3601.         value = do_spec_1 (cpp_spec, 0, NULL_PTR);
  3602.         if (value != 0)
  3603.           return value;
  3604.         break;
  3605.  
  3606.       case 'E':
  3607.         value = do_spec_1 (endfile_spec, 0, NULL_PTR);
  3608.         if (value != 0)
  3609.           return value;
  3610.         break;
  3611.  
  3612.       case 'l':
  3613.         value = do_spec_1 (link_spec, 0, NULL_PTR);
  3614.         if (value != 0)
  3615.           return value;
  3616.         break;
  3617.  
  3618.       case 'L':
  3619.         value = do_spec_1 (lib_spec, 0, NULL_PTR);
  3620.         if (value != 0)
  3621.           return value;
  3622.         break;
  3623.  
  3624.       case 'G':
  3625.         value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
  3626.         if (value != 0)
  3627.           return value;
  3628.         break;
  3629.  
  3630.       case 'p':
  3631.         {
  3632.           char *x = (char *) alloca (strlen (cpp_predefines) + 1);
  3633.           char *buf = x;
  3634.           char *y;
  3635.  
  3636.           /* Copy all of the -D options in CPP_PREDEFINES into BUF.  */
  3637.           y = cpp_predefines;
  3638.           while (*y != 0)
  3639.         {
  3640.           if (! strncmp (y, "-D", 2))
  3641.             /* Copy the whole option.  */
  3642.             while (*y && *y != ' ' && *y != '\t')
  3643.               *x++ = *y++;
  3644.           else if (*y == ' ' || *y == '\t')
  3645.             /* Copy whitespace to the result.  */
  3646.             *x++ = *y++;
  3647.           /* Don't copy other options.  */
  3648.           else
  3649.             y++;
  3650.         }
  3651.  
  3652.           *x = 0;
  3653.  
  3654.           value = do_spec_1 (buf, 0, NULL_PTR);
  3655.           if (value != 0)
  3656.         return value;
  3657.         }
  3658.         break;
  3659.  
  3660.       case 'P':
  3661.         {
  3662.           char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
  3663.           char *buf = x;
  3664.           char *y;
  3665.  
  3666.           /* Copy all of CPP_PREDEFINES into BUF,
  3667.          but put __ after every -D and at the end of each arg.  */
  3668.           y = cpp_predefines;
  3669.           while (*y != 0)
  3670.         {
  3671.           if (! strncmp (y, "-D", 2))
  3672.             {
  3673.               int flag = 0;
  3674.  
  3675.               *x++ = *y++;
  3676.               *x++ = *y++;
  3677.  
  3678.               if (*y != '_'
  3679.               || (*(y+1) != '_' && ! isupper (*(y+1))))
  3680.                 {
  3681.               /* Stick __ at front of macro name.  */
  3682.               *x++ = '_';
  3683.               *x++ = '_';
  3684.               /* Arrange to stick __ at the end as well.  */
  3685.               flag = 1;
  3686.             }
  3687.  
  3688.               /* Copy the macro name.  */
  3689.               while (*y && *y != '=' && *y != ' ' && *y != '\t')
  3690.             *x++ = *y++;
  3691.  
  3692.               if (flag)
  3693.                 {
  3694.               *x++ = '_';
  3695.               *x++ = '_';
  3696.             }
  3697.  
  3698.               /* Copy the value given, if any.  */
  3699.               while (*y && *y != ' ' && *y != '\t')
  3700.             *x++ = *y++;
  3701.             }
  3702.           else if (*y == ' ' || *y == '\t')
  3703.             /* Copy whitespace to the result.  */
  3704.             *x++ = *y++;
  3705.           /* Don't copy -A options  */
  3706.           else
  3707.             y++;
  3708.         }
  3709.           *x++ = ' ';
  3710.  
  3711.           /* Copy all of CPP_PREDEFINES into BUF,
  3712.          but put __ after every -D.  */
  3713.           y = cpp_predefines;
  3714.           while (*y != 0)
  3715.         {
  3716.           if (! strncmp (y, "-D", 2))
  3717.             {
  3718.               y += 2;
  3719.  
  3720.               if (*y != '_'
  3721.               || (*(y+1) != '_' && ! isupper (*(y+1))))
  3722.                 {
  3723.               /* Stick -D__ at front of macro name.  */
  3724.               *x++ = '-';
  3725.               *x++ = 'D';
  3726.               *x++ = '_';
  3727.               *x++ = '_';
  3728.  
  3729.               /* Copy the macro name.  */
  3730.               while (*y && *y != '=' && *y != ' ' && *y != '\t')
  3731.                 *x++ = *y++;
  3732.  
  3733.               /* Copy the value given, if any.  */
  3734.               while (*y && *y != ' ' && *y != '\t')
  3735.                 *x++ = *y++;
  3736.             }
  3737.               else
  3738.             {
  3739.               /* Do not copy this macro - we have just done it before */
  3740.               while (*y && *y != ' ' && *y != '\t')
  3741.                 y++;
  3742.             }
  3743.             }
  3744.           else if (*y == ' ' || *y == '\t')
  3745.             /* Copy whitespace to the result.  */
  3746.             *x++ = *y++;
  3747.           /* Don't copy -A options  */
  3748.           else
  3749.             y++;
  3750.         }
  3751.           *x++ = ' ';
  3752.  
  3753.           /* Copy all of the -A options in CPP_PREDEFINES into BUF.  */
  3754.           y = cpp_predefines;
  3755.           while (*y != 0)
  3756.         {
  3757.           if (! strncmp (y, "-A", 2))
  3758.             /* Copy the whole option.  */
  3759.             while (*y && *y != ' ' && *y != '\t')
  3760.               *x++ = *y++;
  3761.           else if (*y == ' ' || *y == '\t')
  3762.             /* Copy whitespace to the result.  */
  3763.             *x++ = *y++;
  3764.           /* Don't copy other options.  */
  3765.           else
  3766.             y++;
  3767.         }
  3768.  
  3769.           *x = 0;
  3770.  
  3771.           value = do_spec_1 (buf, 0, NULL_PTR);
  3772.           if (value != 0)
  3773.         return value;
  3774.         }
  3775.         break;
  3776.  
  3777.       case 'S':
  3778.         value = do_spec_1 (startfile_spec, 0, NULL_PTR);
  3779.         if (value != 0)
  3780.           return value;
  3781.         break;
  3782.  
  3783.         /* Here we define characters other than letters and digits.  */
  3784.  
  3785.       case '{':
  3786.         p = handle_braces (p);
  3787.         if (p == 0)
  3788.           return -1;
  3789.         break;
  3790.  
  3791.       case '%':
  3792.         obstack_1grow (&obstack, '%');
  3793.         break;
  3794.  
  3795.       case '*':
  3796.         do_spec_1 (soft_matched_part, 1, NULL_PTR);
  3797.         do_spec_1 (" ", 0, NULL_PTR);
  3798.         break;
  3799.  
  3800.         /* Process a string found as the value of a spec given by name.
  3801.            This feature allows individual machine descriptions
  3802.            to add and use their own specs.
  3803.            %[...] modifies -D options the way %P does;
  3804.            %(...) uses the spec unmodified.  */
  3805.       case '(':
  3806.       case '[':
  3807.         {
  3808.           char *name = p;
  3809.           struct spec_list *sl;
  3810.           int len;
  3811.  
  3812.           /* The string after the S/P is the name of a spec that is to be
  3813.          processed. */
  3814.           while (*p && *p != ')' && *p != ']')
  3815.         p++;
  3816.  
  3817.           /* See if it's in the list */
  3818.           for (len = p - name, sl = specs; sl; sl = sl->next)
  3819.         if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
  3820.           {
  3821.             name = sl->spec;
  3822.             break;
  3823.           }
  3824.  
  3825.           if (sl)
  3826.         {
  3827.           if (c == '(')
  3828.             {
  3829.               value = do_spec_1 (name, 0, NULL_PTR);
  3830.               if (value != 0)
  3831.             return value;
  3832.             }
  3833.           else
  3834.             {
  3835.               char *x = (char *) alloca (strlen (name) * 2 + 1);
  3836.               char *buf = x;
  3837.               char *y = name;
  3838.  
  3839.               /* Copy all of NAME into BUF, but put __ after
  3840.              every -D and at the end of each arg,  */
  3841.               while (1)
  3842.             {
  3843.               if (! strncmp (y, "-D", 2))
  3844.                 {
  3845.                   *x++ = '-';
  3846.                   *x++ = 'D';
  3847.                   *x++ = '_';
  3848.                   *x++ = '_';
  3849.                   y += 2;
  3850.                 }
  3851.               else if (*y == ' ' || *y == 0)
  3852.                 {
  3853.                   *x++ = '_';
  3854.                   *x++ = '_';
  3855.                   if (*y == 0)
  3856.                 break;
  3857.                   else
  3858.                 *x++ = *y++;
  3859.                 }
  3860.               else
  3861.                 *x++ = *y++;
  3862.             }
  3863.               *x = 0;
  3864.  
  3865.               value = do_spec_1 (buf, 0, NULL_PTR);
  3866.               if (value != 0)
  3867.             return value;
  3868.             }
  3869.         }
  3870.  
  3871.           /* Discard the closing paren or bracket.  */
  3872.           if (*p)
  3873.         p++;
  3874.         }
  3875.         break;
  3876.  
  3877.       case 'v':
  3878.         {
  3879.           int c1 = *p++;  /* Select first or second version number.  */
  3880.           char *v = compiler_version;
  3881.           char *q, *copy;
  3882.           /* If desired, advance to second version number.  */
  3883.           if (c1 == '2')
  3884.         {
  3885.           /* Set P after the first period.  */
  3886.           while (*v != 0 && *v != ' ' && *v != '.')
  3887.             v++;
  3888.           if (*v == '.')
  3889.             v++;
  3890.         }
  3891.           /* Set Q at the next period or at the end.  */
  3892.           q = v;
  3893.           while (*q != 0 && *q != ' ' && *q != '.')
  3894.         q++;
  3895.           /* Empty string means zero.  */
  3896.           if (p == q)
  3897.         {
  3898.           v = "0";
  3899.           q = v + 1;
  3900.         }
  3901.           /* Put that part into the command.  */
  3902.           obstack_grow (&obstack, v, q - v);
  3903.           arg_going = 1;
  3904.         }
  3905.         break;
  3906.  
  3907.       case '|':
  3908.         if (input_from_pipe)
  3909.           do_spec_1 ("-", 0, NULL_PTR);
  3910.         break;
  3911.  
  3912.       default:
  3913.         abort ();
  3914.       }
  3915.     break;
  3916.  
  3917.       case '\\':
  3918.     /* Backslash: treat next character as ordinary.  */
  3919.     c = *p++;
  3920.  
  3921.     /* fall through */
  3922.       default:
  3923.     /* Ordinary character: put it into the current argument.  */
  3924.     obstack_1grow (&obstack, c);
  3925.     arg_going = 1;
  3926.       }
  3927.  
  3928.   return 0;        /* End of string */
  3929. }
  3930.  
  3931. /* Return 0 if we call do_spec_1 and that returns -1.  */
  3932.  
  3933. static char *
  3934. handle_braces (p)
  3935.      register char *p;
  3936. {
  3937.   register char *q;
  3938.   char *filter;
  3939.   int pipe = 0;
  3940.   int negate = 0;
  3941.   int suffix = 0;
  3942.  
  3943.   if (*p == '|')
  3944.     /* A `|' after the open-brace means,
  3945.        if the test fails, output a single minus sign rather than nothing.
  3946.        This is used in %{|!pipe:...}.  */
  3947.     pipe = 1, ++p;
  3948.  
  3949.   if (*p == '!')
  3950.     /* A `!' after the open-brace negates the condition:
  3951.        succeed if the specified switch is not present.  */
  3952.     negate = 1, ++p;
  3953.  
  3954.   if (*p == '.')
  3955.     /* A `.' after the open-brace means test against the current suffix.  */
  3956.     {
  3957.       if (pipe)
  3958.     abort ();
  3959.  
  3960.       suffix = 1;
  3961.       ++p;
  3962.     }
  3963.  
  3964.   filter = p;
  3965.   while (*p != ':' && *p != '}') p++;
  3966.   if (*p != '}')
  3967.     {
  3968.       register int count = 1;
  3969.       q = p + 1;
  3970.       while (count > 0)
  3971.     {
  3972.       if (*q == '{')
  3973.         count++;
  3974.       else if (*q == '}')
  3975.         count--;
  3976.       else if (*q == 0)
  3977.         abort ();
  3978.       q++;
  3979.     }
  3980.     }
  3981.   else
  3982.     q = p + 1;
  3983.  
  3984.   if (suffix)
  3985.     {
  3986.       int found = (input_suffix != 0
  3987.            && strlen (input_suffix) == p - filter
  3988.            && strncmp (input_suffix, filter, p - filter) == 0);
  3989.  
  3990.       if (p[0] == '}')
  3991.     abort ();
  3992.  
  3993.       if (negate != found
  3994.       && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
  3995.     return 0;
  3996.  
  3997.       return q;
  3998.     }
  3999.   else if (p[-1] == '*' && p[0] == '}')
  4000.     {
  4001.       /* Substitute all matching switches as separate args.  */
  4002.       register int i;
  4003.       --p;
  4004.       for (i = 0; i < n_switches; i++)
  4005.     if (!strncmp (switches[i].part1, filter, p - filter)
  4006.         && check_live_switch (i, p - filter))
  4007.       give_switch (i, 0);
  4008.     }
  4009.   else
  4010.     {
  4011.       /* Test for presence of the specified switch.  */
  4012.       register int i;
  4013.       int present = 0;
  4014.  
  4015.       /* If name specified ends in *, as in {x*:...},
  4016.      check for %* and handle that case.  */
  4017.       if (p[-1] == '*' && !negate)
  4018.     {
  4019.       int substitution;
  4020.       char *r = p;
  4021.  
  4022.       /* First see whether we have %*.  */
  4023.       substitution = 0;
  4024.       while (r < q)
  4025.         {
  4026.           if (*r == '%' && r[1] == '*')
  4027.         substitution = 1;
  4028.           r++;
  4029.         }
  4030.       /* If we do, handle that case.  */
  4031.       if (substitution)
  4032.         {
  4033.           /* Substitute all matching switches as separate args.
  4034.          But do this by substituting for %*
  4035.          in the text that follows the colon.  */
  4036.  
  4037.           unsigned hard_match_len = p - filter - 1;
  4038.           char *string = save_string (p + 1, q - p - 2);
  4039.  
  4040.           for (i = 0; i < n_switches; i++)
  4041.         if (!strncmp (switches[i].part1, filter, hard_match_len)
  4042.             && check_live_switch (i, -1))
  4043.           {
  4044.             do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
  4045.             /* Pass any arguments this switch has.  */
  4046.             give_switch (i, 1);
  4047.           }
  4048.  
  4049.           return q;
  4050.         }
  4051.     }
  4052.  
  4053.       /* If name specified ends in *, as in {x*:...},
  4054.      check for presence of any switch name starting with x.  */
  4055.       if (p[-1] == '*')
  4056.     {
  4057.       for (i = 0; i < n_switches; i++)
  4058.         {
  4059.           unsigned hard_match_len = p - filter - 1;
  4060.  
  4061.           if (!strncmp (switches[i].part1, filter, hard_match_len)
  4062.           && check_live_switch (i, hard_match_len))
  4063.         {
  4064.           present = 1;
  4065.         }
  4066.         }
  4067.     }
  4068.       /* Otherwise, check for presence of exact name specified.  */
  4069.       else
  4070.     {
  4071.       for (i = 0; i < n_switches; i++)
  4072.         {
  4073.           if (!strncmp (switches[i].part1, filter, p - filter)
  4074.           && switches[i].part1[p - filter] == 0
  4075.           && check_live_switch (i, -1))
  4076.         {
  4077.           present = 1;
  4078.           break;
  4079.         }
  4080.         }
  4081.     }
  4082.  
  4083.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  4084.      then substitute either the switch or the specified
  4085.      conditional text.  */
  4086.       if (present != negate)
  4087.     {
  4088.       if (*p == '}')
  4089.         {
  4090.           give_switch (i, 0);
  4091.         }
  4092.       else
  4093.         {
  4094.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
  4095.         return 0;
  4096.         }
  4097.     }
  4098.       else if (pipe)
  4099.     {
  4100.       /* Here if a %{|...} conditional fails: output a minus sign,
  4101.          which means "standard output" or "standard input".  */
  4102.       do_spec_1 ("-", 0, NULL_PTR);
  4103.     }
  4104.     }
  4105.  
  4106.   return q;
  4107. }
  4108.  
  4109. /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
  4110.    on the command line.  PREFIX_LENGTH is the length of XXX in an {XXX*}
  4111.    spec, or -1 if either exact match or %* is used.
  4112.  
  4113.    A -O switch is obsoleted by a later -O switch.  A -f, -m, or -W switch
  4114.    whose value does not begin with "no-" is obsoleted by the same value
  4115.    with the "no-", similarly for a switch with the "no-" prefix.  */
  4116.  
  4117. static int
  4118. check_live_switch (switchnum, prefix_length)
  4119.      int switchnum;
  4120.      int prefix_length;
  4121. {
  4122.   char *name = switches[switchnum].part1;
  4123.   int i;
  4124.  
  4125.   /* In the common case of {<at-most-one-letter>*}, a negating
  4126.      switch would always match, so ignore that case.  We will just
  4127.      send the conflicting switches to the compiler phase.  */
  4128.   if (prefix_length >= 0 && prefix_length <= 1)
  4129.     return 1;
  4130.  
  4131.   /* If we already processed this switch and determined if it was
  4132.      live or not, return our past determination.  */
  4133.   if (switches[switchnum].live_cond != 0)
  4134.     return switches[switchnum].live_cond > 0;
  4135.  
  4136.   /* Now search for duplicate in a manner that depends on the name.  */
  4137.   switch (*name)
  4138.     {
  4139.     case 'O':
  4140.     for (i = switchnum + 1; i < n_switches; i++)
  4141.       if (switches[i].part1[0] == 'O')
  4142.         {
  4143.           switches[switchnum].valid = 1;
  4144.           switches[switchnum].live_cond = -1;
  4145.           return 0;
  4146.         }
  4147.       break;
  4148.  
  4149.     case 'W':  case 'f':  case 'm':
  4150.       if (! strncmp (name + 1, "no-", 3))
  4151.     {
  4152.       /* We have Xno-YYY, search for XYYY. */
  4153.       for (i = switchnum + 1; i < n_switches; i++)
  4154.         if (switches[i].part1[0] == name[0]
  4155.         && ! strcmp (&switches[i].part1[1], &name[4]))
  4156.         {
  4157.           switches[switchnum].valid = 1;
  4158.           switches[switchnum].live_cond = -1;
  4159.           return 0;
  4160.         }
  4161.     }
  4162.       else
  4163.     {
  4164.       /* We have XYYY, search for Xno-YYY.  */
  4165.       for (i = switchnum + 1; i < n_switches; i++)
  4166.         if (switches[i].part1[0] == name[0]
  4167.         && switches[i].part1[1] == 'n'
  4168.         && switches[i].part1[2] == 'o'
  4169.         && switches[i].part1[3] == '-'
  4170.         && !strcmp (&switches[i].part1[4], &name[1]))
  4171.         {
  4172.           switches[switchnum].valid = 1;
  4173.           switches[switchnum].live_cond = -1;
  4174.           return 0;
  4175.         }
  4176.     }
  4177.       break;
  4178.     }
  4179.  
  4180.   /* Otherwise the switch is live.  */
  4181.   switches[switchnum].live_cond = 1;
  4182.   return 1;
  4183. }
  4184.  
  4185. /* Pass a switch to the current accumulating command
  4186.    in the same form that we received it.
  4187.    SWITCHNUM identifies the switch; it is an index into
  4188.    the vector of switches gcc received, which is `switches'.
  4189.    This cannot fail since it never finishes a command line.
  4190.  
  4191.    If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.  */
  4192.  
  4193. static void
  4194. give_switch (switchnum, omit_first_word)
  4195.      int switchnum;
  4196.      int omit_first_word;
  4197. {
  4198.   if (!omit_first_word)
  4199.     {
  4200.       do_spec_1 ("-", 0, NULL_PTR);
  4201.       do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
  4202.     }
  4203.   do_spec_1 (" ", 0, NULL_PTR);
  4204.   if (switches[switchnum].args != 0)
  4205.     {
  4206.       char **p;
  4207.       for (p = switches[switchnum].args; *p; p++)
  4208.     {
  4209.       do_spec_1 (*p, 1, NULL_PTR);
  4210.       do_spec_1 (" ", 0, NULL_PTR);
  4211.     }
  4212.     }
  4213.   switches[switchnum].valid = 1;
  4214. }
  4215.  
  4216. /* Search for a file named NAME trying various prefixes including the
  4217.    user's -B prefix and some standard ones.
  4218.    Return the absolute file name found.  If nothing is found, return NAME.  */
  4219.  
  4220. static char *
  4221. find_file (name)
  4222.      char *name;
  4223. {
  4224.   char *newname;
  4225.  
  4226.   /* Try multilib_dir if it is defined.  */
  4227.   if (multilib_dir != NULL)
  4228.     {
  4229.       char *try;
  4230.  
  4231.       try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
  4232.       strcpy (try, multilib_dir);
  4233.       strcat (try, dir_separator_str);
  4234.       strcat (try, name);
  4235.  
  4236.       newname = find_a_file (&startfile_prefixes, try, R_OK);
  4237.  
  4238.       /* If we don't find it in the multi library dir, then fall
  4239.      through and look for it in the normal places.  */
  4240.       if (newname != NULL)
  4241.     return newname;
  4242.     }
  4243.  
  4244.   newname = find_a_file (&startfile_prefixes, name, R_OK);
  4245.   return newname ? newname : name;
  4246. }
  4247.  
  4248. /* Determine whether a directory exists.  If LINKER, return 0 for
  4249.    certain fixed names not needed by the linker.  If not LINKER, it is
  4250.    only important to return 0 if the host machine has a small ARG_MAX
  4251.    limit.  */
  4252.  
  4253. static int
  4254. is_directory (path1, path2, linker)
  4255.      char *path1;
  4256.      char *path2;
  4257.      int linker;
  4258. {
  4259.   int len1 = strlen (path1);
  4260.   int len2 = strlen (path2);
  4261.   char *path = (char *) alloca (3 + len1 + len2);
  4262.   char *cp;
  4263.   struct stat st;
  4264.  
  4265. #ifndef SMALL_ARG_MAX
  4266.   if (! linker)
  4267.     return 1;
  4268. #endif
  4269.  
  4270.   /* Construct the path from the two parts.  Ensure the string ends with "/.".
  4271.      The resulting path will be a directory even if the given path is a
  4272.      symbolic link.  */
  4273.   bcopy (path1, path, len1);
  4274.   bcopy (path2, path + len1, len2);
  4275.   cp = path + len1 + len2;
  4276.   if (cp[-1] != '/' && cp[-1] != DIR_SEPARATOR)
  4277.     *cp++ = DIR_SEPARATOR;
  4278.   *cp++ = '.';
  4279.   *cp = '\0';
  4280.  
  4281.   /* Exclude directories that the linker is known to search.  */
  4282.   if (linker
  4283.       && ((cp - path == 6
  4284.        && strcmp (path, concat4 (dir_separator_str, "lib", 
  4285.                                      dir_separator_str, ".")) == 0)
  4286.       || (cp - path == 10
  4287.           && strcmp (path, concat6 (dir_separator_str, "usr", 
  4288.                                         dir_separator_str, "lib", 
  4289.                                         dir_separator_str, ".")) == 0)))
  4290.     return 0;
  4291.  
  4292.   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
  4293. }
  4294.  
  4295. /* On fatal signals, delete all the temporary files.  */
  4296.  
  4297. static void
  4298. fatal_error (signum)
  4299.      int signum;
  4300. {
  4301.   signal (signum, SIG_DFL);
  4302.   delete_failure_queue ();
  4303.   delete_temp_files ();
  4304.   /* Get the same signal again, this time not handled,
  4305.      so its normal effect occurs.  */
  4306.   kill (getpid (), signum);
  4307. }
  4308.  
  4309. int
  4310. main (argc, argv)
  4311.      int argc;
  4312.      char **argv;
  4313. {
  4314.   register int i;
  4315.   int j;
  4316.   int value;
  4317.   int linker_was_run = 0;
  4318.   char *explicit_link_files;
  4319.   char *specs_file;
  4320.   char *p;
  4321.  
  4322.   p = argv[0] + strlen (argv[0]);
  4323.   while (p != argv[0] && p[-1] != '/' && p[-1] != DIR_SEPARATOR) --p;
  4324.   programname = p;
  4325.  
  4326.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  4327.     signal (SIGINT, fatal_error);
  4328. #ifdef SIGHUP
  4329.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  4330.     signal (SIGHUP, fatal_error);
  4331. #endif
  4332.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  4333.     signal (SIGTERM, fatal_error);
  4334. #ifdef SIGPIPE
  4335.   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
  4336.     signal (SIGPIPE, fatal_error);
  4337. #endif
  4338.  
  4339.   argbuf_length = 10;
  4340.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  4341.  
  4342.   obstack_init (&obstack);
  4343.  
  4344.   /* Set up to remember the pathname of gcc and any options
  4345.      needed for collect.  We use argv[0] instead of programname because
  4346.      we need the complete pathname.  */
  4347.   obstack_init (&collect_obstack);
  4348.   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
  4349.   obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
  4350.   putenv (obstack_finish (&collect_obstack));
  4351.  
  4352. #ifdef INIT_ENVIRONMENT
  4353.   /* Set up any other necessary machine specific environment variables.  */
  4354.   putenv (INIT_ENVIRONMENT);
  4355. #endif
  4356.  
  4357.   /* Choose directory for temp files.  */
  4358.  
  4359.   choose_temp_base ();
  4360.  
  4361.   /* Make a table of what switches there are (switches, n_switches).
  4362.      Make a table of specified input files (infiles, n_infiles).
  4363.      Decode switches that are handled locally.  */
  4364.  
  4365.   process_command (argc, argv);
  4366.  
  4367.   /* Initialize the vector of specs to just the default.
  4368.      This means one element containing 0s, as a terminator.  */
  4369.  
  4370.   compilers = (struct compiler *) xmalloc (sizeof default_compilers);
  4371.   bcopy ((char *) default_compilers, (char *) compilers,
  4372.      sizeof default_compilers);
  4373.   n_compilers = n_default_compilers;
  4374.  
  4375.   /* Read specs from a file if there is one.  */
  4376.  
  4377.   machine_suffix = concat4 (spec_machine, dir_separator_str,
  4378.                             spec_version, dir_separator_str);
  4379.   just_machine_suffix = concat (spec_machine, dir_separator_str);
  4380.  
  4381.   specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
  4382.   /* Read the specs file unless it is a default one.  */
  4383.   if (specs_file != 0 && strcmp (specs_file, "specs"))
  4384.     read_specs (specs_file);
  4385.  
  4386.   /* If not cross-compiling, look for startfiles in the standard places.  */
  4387.   /* The fact that these are done here, after reading the specs file,
  4388.      means that it cannot be found in these directories.
  4389.      But that's okay.  It should never be there anyway.  */
  4390.   if (!cross_compile)
  4391.     {
  4392. #ifdef MD_EXEC_PREFIX
  4393.       add_prefix (&exec_prefixes, md_exec_prefix, 0, 0, NULL_PTR);
  4394.       add_prefix (&startfile_prefixes, md_exec_prefix, 0, 0, NULL_PTR);
  4395. #endif
  4396.  
  4397. #ifdef MD_STARTFILE_PREFIX
  4398.       add_prefix (&startfile_prefixes, md_startfile_prefix, 0, 0, NULL_PTR);
  4399. #endif
  4400.  
  4401. #ifdef MD_STARTFILE_PREFIX_1
  4402.       add_prefix (&startfile_prefixes, md_startfile_prefix_1, 0, 0, NULL_PTR);
  4403. #endif
  4404.  
  4405.       /* If standard_startfile_prefix is relative, base it on
  4406.      standard_exec_prefix.  This lets us move the installed tree
  4407.      as a unit.  If GCC_EXEC_PREFIX is defined, base
  4408.      standard_startfile_prefix on that as well.  */
  4409.       if (*standard_startfile_prefix == '/'
  4410.       || *standard_startfile_prefix == DIR_SEPARATOR)
  4411.     add_prefix (&startfile_prefixes, standard_startfile_prefix, 0, 0,
  4412.             NULL_PTR);
  4413.       else
  4414.     {
  4415.       if (gcc_exec_prefix)
  4416.         add_prefix (&startfile_prefixes,
  4417.             concat (gcc_exec_prefix, standard_startfile_prefix),
  4418.             0, 0, NULL_PTR);
  4419.       add_prefix (&startfile_prefixes,
  4420.               concat3 (standard_exec_prefix,
  4421.                    machine_suffix,
  4422.                    standard_startfile_prefix),
  4423.               0, 0, NULL_PTR);
  4424.     }               
  4425.  
  4426.       add_prefix (&startfile_prefixes, standard_startfile_prefix_1, 0, 0,
  4427.           NULL_PTR);
  4428.       add_prefix (&startfile_prefixes, standard_startfile_prefix_2, 0, 0,
  4429.           NULL_PTR);
  4430. #if 0 /* Can cause surprises, and one can use -B./ instead.  */
  4431.       add_prefix (&startfile_prefixes, "./", 0, 1, NULL_PTR);
  4432. #endif
  4433.     }
  4434.   else
  4435.     {
  4436.       if (*standard_startfile_prefix != DIR_SEPARATOR && gcc_exec_prefix)
  4437.     add_prefix (&startfile_prefixes,
  4438.             concat (gcc_exec_prefix, standard_startfile_prefix),
  4439.             0, 0, NULL_PTR);
  4440.     }
  4441.  
  4442.   /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake.  */
  4443.   if (gcc_exec_prefix)
  4444.     {
  4445.       char * temp = (char *) xmalloc (strlen (gcc_exec_prefix)
  4446.                       + strlen (spec_version)
  4447.                       + strlen (spec_machine) + 3);
  4448.       strcpy (temp, gcc_exec_prefix);
  4449.       strcat (temp, spec_machine);
  4450.       strcat (temp, dir_separator_str);
  4451.       strcat (temp, spec_version);
  4452.       strcat (temp, dir_separator_str);
  4453.       gcc_exec_prefix = temp;
  4454.     }
  4455.  
  4456.   /* Now we have the specs.
  4457.      Set the `valid' bits for switches that match anything in any spec.  */
  4458.  
  4459.   validate_all_switches ();
  4460.  
  4461.   /* Now that we have the switches and the specs, set
  4462.      the subdirectory based on the options.  */
  4463.   set_multilib_dir ();
  4464.  
  4465.   /* Warn about any switches that no pass was interested in.  */
  4466.  
  4467.   for (i = 0; i < n_switches; i++)
  4468.     if (! switches[i].valid)
  4469.       error ("unrecognized option `-%s'", switches[i].part1);
  4470.  
  4471.   /* Obey some of the options.  */
  4472.  
  4473.   if (print_search_dirs)
  4474.     {
  4475.       printf ("install: %s%s\n", standard_exec_prefix, machine_suffix);
  4476.       printf ("programs: %s\n", build_search_list (&exec_prefixes, "", 0));
  4477.       printf ("libraries: %s\n", build_search_list (&startfile_prefixes, "", 0));
  4478.       exit (0);
  4479.     }
  4480.  
  4481.   if (print_file_name)
  4482.     {
  4483.       printf ("%s\n", find_file (print_file_name));
  4484.       exit (0);
  4485.     }
  4486.  
  4487.   if (print_prog_name)
  4488.     {
  4489.       char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
  4490.       printf ("%s\n", (newname ? newname : print_prog_name));
  4491.       exit (0);
  4492.     }
  4493.  
  4494.   if (print_multi_lib)
  4495.     {
  4496.       print_multilib_info ();
  4497.       exit (0);
  4498.     }
  4499.  
  4500.   if (print_multi_directory)
  4501.     {
  4502.       if (multilib_dir == NULL)
  4503.     printf (".\n");
  4504.       else
  4505.     printf ("%s\n", multilib_dir);
  4506.       exit (0);
  4507.     }
  4508.  
  4509.   if (verbose_flag)
  4510.     {
  4511.       if (! strcmp (version_string, compiler_version))
  4512.     fprintf (stderr, "gcc version %s\n", version_string);
  4513.       else
  4514.     fprintf (stderr, "gcc driver version %s executing gcc version %s\n",
  4515.          version_string, compiler_version);
  4516.  
  4517.       if (n_infiles == 0)
  4518.     exit (0);
  4519.     }
  4520.  
  4521.   if (n_infiles == 0)
  4522.     fatal ("No input files");
  4523.  
  4524.   /* Make a place to record the compiler output file names
  4525.      that correspond to the input files.  */
  4526.  
  4527.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  4528.   bzero ((char *) outfiles, n_infiles * sizeof (char *));
  4529.  
  4530.   /* Record which files were specified explicitly as link input.  */
  4531.  
  4532.   explicit_link_files = xmalloc (n_infiles);
  4533.   bzero (explicit_link_files, n_infiles);
  4534.  
  4535.   for (i = 0; i < n_infiles; i++)
  4536.     {
  4537.       register struct compiler *cp = 0;
  4538.       int this_file_error = 0;
  4539.  
  4540.       /* Tell do_spec what to substitute for %i.  */
  4541.  
  4542.       input_filename = infiles[i].name;
  4543.       input_filename_length = strlen (input_filename);
  4544.       input_file_number = i;
  4545.  
  4546.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  4547.  
  4548.       outfiles[i] = input_filename;
  4549.  
  4550.       /* Figure out which compiler from the file's suffix.  */
  4551.  
  4552.       cp = lookup_compiler (infiles[i].name, input_filename_length,
  4553.                 infiles[i].language);
  4554.  
  4555.       if (cp)
  4556.     {
  4557.       /* Ok, we found an applicable compiler.  Run its spec.  */
  4558.       /* First say how much of input_filename to substitute for %b  */
  4559.       register char *p;
  4560.       int len;
  4561.  
  4562. #ifdef FILE_NAME_NONDIRECTORY
  4563.       input_basename = FILE_NAME_NONDIRECTORY (input_filename);
  4564. #else
  4565.       input_basename = input_filename;
  4566.       for (p = input_filename; *p; p++)
  4567.         if (*p == '/' || *p == DIR_SEPARATOR)
  4568.           input_basename = p + 1;
  4569. #endif
  4570.  
  4571.       /* Find a suffix starting with the last period,
  4572.          and set basename_length to exclude that suffix.  */
  4573.       basename_length = strlen (input_basename);
  4574.       p = input_basename + basename_length;
  4575.       while (p != input_basename && *p != '.') --p;
  4576.       if (*p == '.' && p != input_basename)
  4577.         {
  4578.           basename_length = p - input_basename;
  4579.           input_suffix = p + 1;
  4580.         }
  4581.       else
  4582.         input_suffix = "";
  4583.  
  4584.       len = 0;
  4585.       for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
  4586.         if (cp->spec[j])
  4587.           len += strlen (cp->spec[j]);
  4588.  
  4589.       p = (char *) xmalloc (len + 1);
  4590.  
  4591.       len = 0;
  4592.       for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
  4593.         if (cp->spec[j])
  4594.           {
  4595.         strcpy (p + len, cp->spec[j]);
  4596.         len += strlen (cp->spec[j]);
  4597.           }
  4598.  
  4599.       value = do_spec (p);
  4600.       free (p);
  4601.       if (value < 0)
  4602.         this_file_error = 1;
  4603.     }
  4604.  
  4605.       /* If this file's name does not contain a recognized suffix,
  4606.      record it as explicit linker input.  */
  4607.  
  4608.       else
  4609.     explicit_link_files[i] = 1;
  4610.  
  4611.       /* Clear the delete-on-failure queue, deleting the files in it
  4612.      if this compilation failed.  */
  4613.  
  4614.       if (this_file_error)
  4615.     {
  4616.       delete_failure_queue ();
  4617.       error_count++;
  4618.     }
  4619.       /* If this compilation succeeded, don't delete those files later.  */
  4620.       clear_failure_queue ();
  4621.     }
  4622.  
  4623.   /* Run ld to link all the compiler output files.  */
  4624.  
  4625.   if (error_count == 0)
  4626.     {
  4627.       int tmp = execution_count;
  4628.       int i;
  4629.       int first_time;
  4630.  
  4631.       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
  4632.      for collect.  */
  4633.       putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH=");
  4634.       putenv_from_prefixes (&startfile_prefixes, "LIBRARY_PATH=");
  4635.  
  4636.       /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
  4637.      the compiler.  */
  4638.       obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
  4639.             sizeof ("COLLECT_GCC_OPTIONS=")-1);
  4640.  
  4641.       first_time = TRUE;
  4642.       for (i = 0; i < n_switches; i++)
  4643.     {
  4644.       char **args;
  4645.       if (!first_time)
  4646.         obstack_grow (&collect_obstack, " ", 1);
  4647.  
  4648.       first_time = FALSE;
  4649.       obstack_grow (&collect_obstack, "-", 1);
  4650.       obstack_grow (&collect_obstack, switches[i].part1,
  4651.             strlen (switches[i].part1));
  4652.  
  4653.       for (args = switches[i].args; args && *args; args++)
  4654.         {
  4655.           obstack_grow (&collect_obstack, " ", 1);
  4656.           obstack_grow (&collect_obstack, *args, strlen (*args));
  4657.         }
  4658.     }
  4659.       obstack_grow (&collect_obstack, "\0", 1);
  4660.       putenv (obstack_finish (&collect_obstack));
  4661.  
  4662.       value = do_spec (link_command_spec);
  4663.       if (value < 0)
  4664.     error_count = 1;
  4665.       linker_was_run = (tmp != execution_count);
  4666.     }
  4667.  
  4668.   /* Warn if a -B option was specified but the prefix was never used.  */
  4669.   unused_prefix_warnings (&exec_prefixes);
  4670.   unused_prefix_warnings (&startfile_prefixes);
  4671.  
  4672.   /* If options said don't run linker,
  4673.      complain about input files to be given to the linker.  */
  4674.  
  4675.   if (! linker_was_run && error_count == 0)
  4676.     for (i = 0; i < n_infiles; i++)
  4677.       if (explicit_link_files[i])
  4678.     error ("%s: linker input file unused since linking not done",
  4679.            outfiles[i]);
  4680.  
  4681.   /* Delete some or all of the temporary files we made.  */
  4682.  
  4683.   if (error_count)
  4684.     delete_failure_queue ();
  4685.   delete_temp_files ();
  4686.  
  4687.   exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
  4688.   /* NOTREACHED */
  4689.   return 0;
  4690. }
  4691.  
  4692. /* Find the proper compilation spec for the file name NAME,
  4693.    whose length is LENGTH.  LANGUAGE is the specified language,
  4694.    or 0 if none specified.  */
  4695.  
  4696. static struct compiler *
  4697. lookup_compiler (name, length, language)
  4698.      char *name;
  4699.      int length;
  4700.      char *language;
  4701. {
  4702.   struct compiler *cp;
  4703.  
  4704.   /* Look for the language, if one is spec'd.  */
  4705.   if (language != 0)
  4706.     {
  4707.       for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
  4708.     {
  4709.       if (language != 0)
  4710.         {
  4711.           if (cp->suffix[0] == '@'
  4712.           && !strcmp (cp->suffix + 1, language))
  4713.         return cp;
  4714.         }
  4715.     }
  4716.       error ("language %s not recognized", language);
  4717.     }
  4718.  
  4719.   /* Look for a suffix.  */
  4720.   for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
  4721.     {
  4722.       if (/* The suffix `-' matches only the file name `-'.  */
  4723.       (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
  4724.       ||
  4725.       (strlen (cp->suffix) < length
  4726.        /* See if the suffix matches the end of NAME.  */
  4727. #ifdef OS2
  4728.        && (!strcmp (cp->suffix,
  4729.             name + length - strlen (cp->suffix))
  4730.         || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  4731.          && !strcasecmp (cp->suffix,
  4732.               name + length - strlen (cp->suffix)))))
  4733. #else
  4734.        && !strcmp (cp->suffix,
  4735.                name + length - strlen (cp->suffix))))
  4736. #endif
  4737.     {
  4738.       if (cp->spec[0][0] == '@')
  4739.         {
  4740.           struct compiler *new;
  4741.           /* An alias entry maps a suffix to a language.
  4742.          Search for the language; pass 0 for NAME and LENGTH
  4743.          to avoid infinite recursion if language not found.
  4744.          Construct the new compiler spec.  */
  4745.           language = cp->spec[0] + 1;
  4746.           new = (struct compiler *) xmalloc (sizeof (struct compiler));
  4747.           new->suffix = cp->suffix;
  4748.           bcopy ((char *) lookup_compiler (NULL_PTR, 0, language)->spec,
  4749.              (char *) new->spec, sizeof new->spec);
  4750.           return new;
  4751.         }
  4752.       /* A non-alias entry: return it.  */
  4753.       return cp;
  4754.     }
  4755.     }
  4756.  
  4757.   return 0;
  4758. }
  4759.  
  4760. char *
  4761. xmalloc (size)
  4762.      unsigned size;
  4763. {
  4764.   register char *value = (char *) malloc (size);
  4765.   if (value == 0)
  4766.     fatal ("virtual memory exhausted");
  4767.   return value;
  4768. }
  4769.  
  4770. char *
  4771. xrealloc (ptr, size)
  4772.      char *ptr;
  4773.      unsigned size;
  4774. {
  4775.   register char *value = (char *) realloc (ptr, size);
  4776.   if (value == 0)
  4777.     fatal ("virtual memory exhausted");
  4778.   return value;
  4779. }
  4780.  
  4781. /* Return a newly-allocated string whose contents concatenate those of s1, s2 */
  4782.  
  4783. static char *
  4784. concat (s1, s2)
  4785.      char *s1, *s2;
  4786. {
  4787.   int len1 = strlen (s1);
  4788.   int len2 = strlen (s2);
  4789.   char *result = xmalloc (len1 + len2 + 1);
  4790.  
  4791.   strcpy (result, s1);
  4792.   strcpy (result + len1, s2);
  4793.   *(result + len1 + len2) = 0;
  4794.  
  4795.   return result;
  4796. }
  4797.  
  4798. static char *
  4799. concat3 (s1, s2, s3)
  4800.      char *s1, *s2, *s3;
  4801. {
  4802.   return concat (concat (s1, s2), s3);
  4803. }
  4804.  
  4805. static char *
  4806. concat4 (s1, s2, s3, s4)
  4807.      char *s1, *s2, *s3, *s4;
  4808. {
  4809.   return concat (concat (s1, s2), concat (s3, s4));
  4810. }
  4811.  
  4812. static char *
  4813. concat6 (s1, s2, s3, s4, s5, s6)
  4814.      char *s1, *s2, *s3, *s4, *s5, *s6;
  4815. {
  4816.   return concat3 (concat (s1, s2), concat (s3, s4), concat (s5, s6));
  4817. }
  4818.  
  4819. static char *
  4820. save_string (s, len)
  4821.      char *s;
  4822.      int len;
  4823. {
  4824.   register char *result = xmalloc (len + 1);
  4825.  
  4826.   bcopy (s, result, len);
  4827.   result[len] = 0;
  4828.   return result;
  4829. }
  4830.  
  4831. static void
  4832. pfatal_with_name (name)
  4833.      char *name;
  4834. {
  4835.   char *s;
  4836.  
  4837.   if (errno < sys_nerr)
  4838.     s = concat ("%s: ", my_strerror( errno ));
  4839.   else
  4840.     s = "cannot open %s";
  4841.   fatal (s, name);
  4842. }
  4843.  
  4844. static void
  4845. perror_with_name (name)
  4846.      char *name;
  4847. {
  4848.   char *s;
  4849.  
  4850.   if (errno < sys_nerr)
  4851.     s = concat ("%s: ", my_strerror( errno ));
  4852.   else
  4853.     s = "cannot open %s";
  4854.   error (s, name);
  4855. }
  4856.  
  4857. static void
  4858. perror_exec (name)
  4859.      char *name;
  4860. {
  4861.   char *s;
  4862.  
  4863.   if (errno < sys_nerr)
  4864.     s = concat ("installation problem, cannot exec %s: ", my_strerror( errno ));
  4865.   else
  4866.     s = "installation problem, cannot exec %s";
  4867.   error (s, name);
  4868. }
  4869.  
  4870. /* More 'friendly' abort that prints the line and file.
  4871.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  4872.  
  4873. void
  4874. fancy_abort ()
  4875. {
  4876.   fatal ("Internal gcc abort.");
  4877. }
  4878.  
  4879. #ifdef HAVE_VPRINTF
  4880.  
  4881. /* Output an error message and exit */
  4882.  
  4883. static void
  4884. fatal VPROTO((char *format, ...))
  4885. {
  4886. #ifndef __STDC__
  4887.   char *format;
  4888. #endif
  4889.   va_list ap;
  4890.  
  4891.   VA_START (ap, format);
  4892.  
  4893. #ifndef __STDC__
  4894.   format = va_arg (ap, char*);
  4895. #endif
  4896.  
  4897.   fprintf (stderr, "%s: ", programname);
  4898.   vfprintf (stderr, format, ap);
  4899.   va_end (ap);
  4900.   fprintf (stderr, "\n");
  4901.   delete_temp_files ();
  4902.   exit (1);
  4903. }
  4904.  
  4905. static void
  4906. error VPROTO((char *format, ...))
  4907. {
  4908. #ifndef __STDC__
  4909.   char *format;
  4910. #endif
  4911.   va_list ap;
  4912.  
  4913.   VA_START (ap, format);
  4914.  
  4915. #ifndef __STDC__
  4916.   format = va_arg (ap, char*);
  4917. #endif
  4918.  
  4919.   fprintf (stderr, "%s: ", programname);
  4920.   vfprintf (stderr, format, ap);
  4921.   va_end (ap);
  4922.  
  4923.   fprintf (stderr, "\n");
  4924. }
  4925.  
  4926. #else /* not HAVE_VPRINTF */
  4927.  
  4928. static void
  4929. fatal (msg, arg1, arg2)
  4930.      char *msg, *arg1, *arg2;
  4931. {
  4932.   error (msg, arg1, arg2);
  4933.   delete_temp_files ();
  4934.   exit (1);
  4935. }
  4936.  
  4937. static void
  4938. error (msg, arg1, arg2)
  4939.      char *msg, *arg1, *arg2;
  4940. {
  4941.   fprintf (stderr, "%s: ", programname);
  4942.   fprintf (stderr, msg, arg1, arg2);
  4943.   fprintf (stderr, "\n");
  4944. }
  4945.  
  4946. #endif /* not HAVE_VPRINTF */
  4947.  
  4948.  
  4949. static void
  4950. validate_all_switches ()
  4951. {
  4952.   struct compiler *comp;
  4953.   register char *p;
  4954.   register char c;
  4955.   struct spec_list *spec;
  4956.  
  4957.   for (comp = compilers; comp->spec[0]; comp++)
  4958.     {
  4959.       int i;
  4960.       for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
  4961.     {
  4962.       p = comp->spec[i];
  4963.       while (c = *p++)
  4964.         if (c == '%' && *p == '{')
  4965.           /* We have a switch spec.  */
  4966.           validate_switches (p + 1);
  4967.     }
  4968.     }
  4969.  
  4970.   /* look through the linked list of extra specs read from the specs file */
  4971.   for (spec = specs; spec ; spec = spec->next)
  4972.     {
  4973.       p = spec->spec;
  4974.       while (c = *p++)
  4975.     if (c == '%' && *p == '{')
  4976.       /* We have a switch spec.  */
  4977.       validate_switches (p + 1);
  4978.     }
  4979.  
  4980.   p = link_command_spec;
  4981.   while (c = *p++)
  4982.     if (c == '%' && *p == '{')
  4983.       /* We have a switch spec.  */
  4984.       validate_switches (p + 1);
  4985.  
  4986.   /* Now notice switches mentioned in the machine-specific specs.  */
  4987.  
  4988.   p = asm_spec;
  4989.   while (c = *p++)
  4990.     if (c == '%' && *p == '{')
  4991.       /* We have a switch spec.  */
  4992.       validate_switches (p + 1);
  4993.  
  4994.   p = asm_final_spec;
  4995.   while (c = *p++)
  4996.     if (c == '%' && *p == '{')
  4997.       /* We have a switch spec.  */
  4998.       validate_switches (p + 1);
  4999.  
  5000.   p = cpp_spec;
  5001.   while (c = *p++)
  5002.     if (c == '%' && *p == '{')
  5003.       /* We have a switch spec.  */
  5004.       validate_switches (p + 1);
  5005.  
  5006.   p = signed_char_spec;
  5007.   while (c = *p++)
  5008.     if (c == '%' && *p == '{')
  5009.       /* We have a switch spec.  */
  5010.       validate_switches (p + 1);
  5011.  
  5012.   p = cc1_spec;
  5013.   while (c = *p++)
  5014.     if (c == '%' && *p == '{')
  5015.       /* We have a switch spec.  */
  5016.       validate_switches (p + 1);
  5017.  
  5018.   p = cc1plus_spec;
  5019.   while (c = *p++)
  5020.     if (c == '%' && *p == '{')
  5021.       /* We have a switch spec.  */
  5022.       validate_switches (p + 1);
  5023.  
  5024.   p = link_spec;
  5025.   while (c = *p++)
  5026.     if (c == '%' && *p == '{')
  5027.       /* We have a switch spec.  */
  5028.       validate_switches (p + 1);
  5029.  
  5030.   p = lib_spec;
  5031.   while (c = *p++)
  5032.     if (c == '%' && *p == '{')
  5033.       /* We have a switch spec.  */
  5034.       validate_switches (p + 1);
  5035.  
  5036.   p = libgcc_spec;
  5037.   while (c = *p++)
  5038.     if (c == '%' && *p == '{')
  5039.       /* We have a switch spec.  */
  5040.       validate_switches (p + 1);
  5041.  
  5042.   p = startfile_spec;
  5043.   while (c = *p++)
  5044.     if (c == '%' && *p == '{')
  5045.       /* We have a switch spec.  */
  5046.       validate_switches (p + 1);
  5047. }
  5048.  
  5049. /* Look at the switch-name that comes after START
  5050.    and mark as valid all supplied switches that match it.  */
  5051.  
  5052. static void
  5053. validate_switches (start)
  5054.      char *start;
  5055. {
  5056.   register char *p = start;
  5057.   char *filter;
  5058.   register int i;
  5059.   int suffix = 0;
  5060.  
  5061.   if (*p == '|')
  5062.     ++p;
  5063.  
  5064.   if (*p == '!')
  5065.     ++p;
  5066.  
  5067.   if (*p == '.')
  5068.     suffix = 1, ++p;
  5069.  
  5070.   filter = p;
  5071.   while (*p != ':' && *p != '}') p++;
  5072.  
  5073.   if (suffix)
  5074.     ;
  5075.   else if (p[-1] == '*')
  5076.     {
  5077.       /* Mark all matching switches as valid.  */
  5078.       --p;
  5079.       for (i = 0; i < n_switches; i++)
  5080.     if (!strncmp (switches[i].part1, filter, p - filter))
  5081.       switches[i].valid = 1;
  5082.     }
  5083.   else
  5084.     {
  5085.       /* Mark an exact matching switch as valid.  */
  5086.       for (i = 0; i < n_switches; i++)
  5087.     {
  5088.       if (!strncmp (switches[i].part1, filter, p - filter)
  5089.           && switches[i].part1[p - filter] == 0)
  5090.         switches[i].valid = 1;
  5091.     }
  5092.     }
  5093. }
  5094.  
  5095. /* Check whether a particular argument was used.  */
  5096.  
  5097. static int
  5098. used_arg (p, len)
  5099.      char *p;
  5100.      int len;
  5101. {
  5102.   int i;
  5103.  
  5104.   for (i = 0; i < n_switches; i++)
  5105.     if (! strncmp (switches[i].part1, p, len)
  5106.     && strlen (switches[i].part1) == len)
  5107.       return 1;
  5108.   return 0;
  5109. }
  5110.  
  5111. /* Work out the subdirectory to use based on the
  5112.    options.  The format of multilib_select is a list of elements.
  5113.    Each element is a subdirectory name followed by a list of options
  5114.    followed by a semicolon.  gcc will consider each line in turn.  If
  5115.    none of the options beginning with an exclamation point are
  5116.    present, and all of the other options are present, that
  5117.    subdirectory will be used.  */
  5118.  
  5119. static void
  5120. set_multilib_dir ()
  5121. {
  5122.   char *p = multilib_select;
  5123.   int this_path_len;
  5124.   char *this_path, *this_arg;
  5125.   int failed;
  5126.  
  5127.   while (*p != '\0')
  5128.     {
  5129.       /* Ignore newlines.  */
  5130.       if (*p == '\n')
  5131.     {
  5132.       ++p;
  5133.       continue;
  5134.     }
  5135.  
  5136.       /* Get the initial path.  */
  5137.       this_path = p;
  5138.       while (*p != ' ')
  5139.     {
  5140.       if (*p == '\0')
  5141.         abort ();
  5142.       ++p;
  5143.     }
  5144.       this_path_len = p - this_path;
  5145.  
  5146.       /* Check the arguments.  */
  5147.       failed = 0;
  5148.       ++p;
  5149.       while (*p != ';')
  5150.     {
  5151.       if (*p == '\0')
  5152.         abort ();
  5153.  
  5154.       if (failed)
  5155.         {
  5156.           ++p;
  5157.           continue;
  5158.         }
  5159.  
  5160.       this_arg = p;
  5161.       while (*p != ' ' && *p != ';')
  5162.         {
  5163.           if (*p == '\0')
  5164.         abort ();
  5165.           ++p;
  5166.         }
  5167.  
  5168.       if (*this_arg == '!')
  5169.         failed = used_arg (this_arg + 1, p - (this_arg + 1));
  5170.       else
  5171.         failed = ! used_arg (this_arg, p - this_arg);
  5172.  
  5173.       if (*p == ' ')
  5174.         ++p;
  5175.     }
  5176.  
  5177.       if (! failed)
  5178.     {
  5179.       if (this_path_len != 1
  5180.           || this_path[0] != '.')
  5181.         {
  5182.           multilib_dir = xmalloc (this_path_len + 1);
  5183.           strncpy (multilib_dir, this_path, this_path_len);
  5184.           multilib_dir[this_path_len] = '\0';
  5185.         }
  5186.       break;
  5187.     }
  5188.  
  5189.       ++p;
  5190.     }      
  5191. }
  5192.  
  5193. /* Print out the multiple library subdirectory selection
  5194.    information.  This prints out a series of lines.  Each line looks
  5195.    like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
  5196.    required.  Only the desired options are printed out, the negative
  5197.    matches.  The options are print without a leading dash.  There are
  5198.    no spaces to make it easy to use the information in the shell.
  5199.    Each subdirectory is printed only once.  This assumes the ordering
  5200.    generated by the genmultilib script.  */
  5201.  
  5202. static void
  5203. print_multilib_info ()
  5204. {
  5205.   char *p = multilib_select;
  5206.   char *last_path = 0, *this_path;
  5207.   int skip, use_arg;
  5208.   int last_path_len = 0;
  5209.  
  5210.   while (*p != '\0')
  5211.     {
  5212.       /* Ignore newlines.  */
  5213.       if (*p == '\n')
  5214.     {
  5215.       ++p;
  5216.       continue;
  5217.     }
  5218.  
  5219.       /* Get the initial path.  */
  5220.       this_path = p;
  5221.       while (*p != ' ')
  5222.     {
  5223.       if (*p == '\0')
  5224.         abort ();
  5225.       ++p;
  5226.     }
  5227.  
  5228.       /* If this is a duplicate, skip it.  */
  5229.       skip = (last_path != 0 && p - this_path == last_path_len
  5230.           && ! strncmp (last_path, this_path, last_path_len));
  5231.  
  5232.       last_path = this_path;
  5233.       last_path_len = p - this_path;
  5234.  
  5235.       if (! skip)
  5236.     {
  5237.       char *p1;
  5238.  
  5239.       for (p1 = last_path; p1 < p; p1++)
  5240.         putchar (*p1);
  5241.       putchar (';');
  5242.     }
  5243.  
  5244.       ++p;
  5245.       while (*p != ';')
  5246.     {
  5247.       int use_arg;
  5248.  
  5249.       if (*p == '\0')
  5250.         abort ();
  5251.  
  5252.       if (skip)
  5253.         {
  5254.           ++p;
  5255.           continue;
  5256.         }
  5257.  
  5258.       use_arg = *p != '!';
  5259.  
  5260.       if (use_arg)
  5261.         putchar ('@');
  5262.  
  5263.       while (*p != ' ' && *p != ';')
  5264.         {
  5265.           if (*p == '\0')
  5266.         abort ();
  5267.           if (use_arg)
  5268.         putchar (*p);
  5269.           ++p;
  5270.         }
  5271.  
  5272.       if (*p == ' ')
  5273.         ++p;
  5274.     }
  5275.  
  5276.       if (! skip)
  5277.     putchar ('\n');
  5278.  
  5279.       ++p;
  5280.     }
  5281. }
  5282.